Skip to content

Commit b558fbc

Browse files
committed
initial commit
0 parents  commit b558fbc

File tree

2 files changed

+359
-0
lines changed

2 files changed

+359
-0
lines changed

forEachMapFilter.js

Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
1+
/*
2+
Write a function called doubleValues which accepts an array and returns a new array with all the values in the array passed to the function doubled
3+
4+
Examples:
5+
doubleValues([1,2,3]) // [2,4,6]
6+
doubleValues([5,1,2,3,10]) // [10,2,4,6,20]
7+
8+
*/
9+
function doubleValues(arr){
10+
const newArr = [];
11+
12+
arr.forEach(function(char) {
13+
newArr.push(char += char);
14+
})
15+
16+
return newArr;
17+
}
18+
19+
/*
20+
Write a function called onlyEvenValues which accepts an array and returns a new array with only the even values in the array passed to the function
21+
22+
Examples:
23+
onlyEvenValues([1,2,3]) // [2]
24+
onlyEvenValues([5,1,2,3,10]) // [2,10]
25+
26+
*/
27+
function onlyEvenValues(arr){
28+
const newArr = [];
29+
30+
arr.forEach(function(char) {
31+
if (char % 2 === 0) {
32+
newArr.push(char);
33+
}
34+
return;
35+
})
36+
37+
return newArr;
38+
}
39+
40+
/*
41+
Write a function called showFirstAndLast which accepts an array of strings and returns a new array with only the first and last character of each string.
42+
43+
Examples:
44+
showFirstAndLast(['colt','matt', 'tim', 'test']) // ["ct", "mt", "tm", "tt"]
45+
showFirstAndLast(['hi', 'goodbye', 'smile']) // ['hi', 'ge', 'se']
46+
47+
*/
48+
function showFirstAndLast(arr){
49+
const newArr = [];
50+
51+
arr.forEach(function(str) {
52+
const length = (str.length)-1;
53+
const first = str[0];
54+
const last = str[length];
55+
56+
newArr.push(first + last);
57+
})
58+
59+
return newArr;
60+
}
61+
62+
/*
63+
Write a function called addKeyAndValue which accepts an array of objects, a key, and a value and returns the array passed to the function with the new key and value added for each object
64+
65+
Examples:
66+
addKeyAndValue([{name: 'Elie'}, {name: 'Tim'}, {name: 'Matt'}, {name: 'Colt'}], 'title', 'instructor')
67+
68+
// [{name: 'Elie', title:'instructor'}, {name: 'Tim', title:'instructor'}, {name: 'Matt', title:'instructor'}, {name: 'Colt', title:'instructor'}]
69+
70+
*/
71+
function addKeyAndValue(arr,key,value){
72+
const newArr = [];
73+
const title = key;
74+
const val = value;
75+
76+
arr.forEach(function(obj) {
77+
obj.title = val;
78+
newArr.push(obj);
79+
})
80+
81+
return newArr;
82+
}
83+
84+
/*
85+
Write a function called vowelCount which accepts a string and returns an object with the keys as the vowel and the values as the number of times the vowel appears in the string. This function should be case insensitive so a lowercase letter and uppercase letter should count
86+
87+
Examples:
88+
vowelCount('Elie') // {e:2,i:1};
89+
vowelCount('Tim') // {i:1};
90+
vowelCount('Matt') // {a:1})
91+
vowelCount('hmmm') // {};
92+
vowelCount('I Am awesome and so are you') // {i: 1, a: 4, e: 3, o: 3, u: 1};
93+
*/
94+
function vowelCount(str){
95+
const newObj = {};
96+
const lowerCaseString = str.toLowerCase();
97+
98+
const isAVowel = function(ltr) {
99+
return "aeiou".indexOf(ltr) !== -1;
100+
}
101+
102+
for (let char of lowerCaseString) {
103+
if (isAVowel(char)) {
104+
if (newObj[char]) {
105+
newObj[char]++;
106+
}
107+
else {newObj[char] = 1;}
108+
}
109+
}
110+
return newObj;
111+
}
112+
113+
/*
114+
Write a function called doubleValuesWithMap which accepts an array and returns a new array with all the values in the array passed to the function doubled
115+
116+
Examples:
117+
doubleValuesWithMap([1,2,3]) // [2,4,6]
118+
doubleValuesWithMap([1,-2,-3]) // [2,-4,-6]
119+
*/
120+
121+
function doubleValuesWithMap(arr) {
122+
return arr.map(function(value) {
123+
return (value + value);
124+
})
125+
}
126+
/*
127+
Write a function called valTimesIndex which accepts an array and returns a new array with each value multiplied by the index it is currently at in the array.
128+
129+
Examples:
130+
valTimesIndex([1,2,3]) // [0,2,6]
131+
valTimesIndex([1,-2,-3]) // [0,-2,-6]
132+
*/
133+
134+
function valTimesIndex(arr){
135+
return arr.map(function (value, i){
136+
return value * i;
137+
})
138+
}
139+
140+
/*
141+
Write a function called extractKey which accepts an array of objects and some key and returns a new array with the value of that key in each object.
142+
143+
Examples:
144+
extractKey([{name: 'Elie'}, {name: 'Tim'}, {name: 'Matt'}, {name: 'Colt'}], 'name') // ['Elie', 'Tim', 'Matt', 'Colt']
145+
*/
146+
147+
function extractKey(arr, key){
148+
return arr.map(function(obj) {
149+
return obj[key];
150+
})
151+
}
152+
153+
/*
154+
Write a function called extractFullName which accepts an array of objects and returns a new array with the value of the key with a name of "first" and the value of a key with the name of "last" in each object, concatenated together with a space.
155+
156+
Examples:
157+
extractFullName([{first: 'Elie', last:"Schoppik"}, {first: 'Tim', last:"Garcia"}, {first: 'Matt', last:"Lane"}, {first: 'Colt', last:"Steele"}]) // ['Elie Schoppik', 'Tim Garcia', 'Matt Lane', 'Colt Steele']
158+
*/
159+
160+
function extractFullName(arr){
161+
return arr.map(function(obj) {
162+
return (obj["first"] + " " + obj["last"]);
163+
})
164+
}
165+
166+
/*
167+
Write a function called filterByValue which accepts an array of objects and a key and returns a new array with all the objects that contain that key.
168+
169+
Examples:
170+
filterByValue([{first: 'Elie', last:"Schoppik"}, {first: 'Tim', last:"Garcia", isCatOwner: true}, {first: 'Matt', last:"Lane"}, {first: 'Colt', last:"Steele", isCatOwner: true}], 'isCatOwner') // [{first: 'Tim', last:"Garcia", isCatOwner: true}, {first: 'Colt', last:"Steele", isCatOwner: true}]
171+
*/
172+
173+
function filterByValue(arr, key) {
174+
return arr.filter(function(obj) {
175+
if (obj[key]) {
176+
return obj;
177+
}
178+
})
179+
}
180+
181+
/*
182+
Write a function called find which accepts an array and a value and returns the first element in the array that has the same value as the second parameter or undefined if the value is not found in the array.
183+
184+
Examples:
185+
find([1,2,3,4,5], 3) // 3
186+
find([1,2,3,4,5], 10) // undefined
187+
*/
188+
189+
function find(arr, searchValue) {
190+
return arr.filter(function(value) {
191+
return value === searchValue;
192+
})
193+
[0];
194+
}
195+
196+
/*
197+
Write a function called findInObj which accepts an array of objects, a key, and some value to search for
198+
and returns the first found value in the array.
199+
200+
Examples:
201+
findInObj([{first: 'Elie', last:"Schoppik"}, {first: 'Tim', last:"Garcia", isCatOwner: true}, {first: 'Matt', last:"Lane"}, {first: 'Colt', last:"Steele", isCatOwner: true}], 'isCatOwner',true) // {first: 'Tim', last:"Garcia", isCatOwner: true}
202+
*/
203+
204+
function findInObj(arr, key, searchValue) {
205+
return arr.filter(function(obj) {
206+
return obj[key] === searchValue
207+
})
208+
[0]
209+
}
210+
211+
/*
212+
Write a function called removeVowels which accepts a string and returns a new string with all of the vowels
213+
(both uppercased and lowercased) removed. Every character in the new string should be lowercased.
214+
215+
Examples:
216+
removeVowels('Elie') // ('l')
217+
removeVowels('TIM') // ('tm')
218+
removeVowels('ZZZZZZ') // ('zzzzzz')
219+
*/
220+
221+
function removeVowels(str) {
222+
const lowerCaseStr = Array.from(str.toLowerCase());
223+
224+
return lowerCaseStr.filter(function(char) {
225+
return ("aeiou".indexOf(char) === -1);
226+
})
227+
.join("");
228+
}
229+
230+
/*
231+
Write a function called doubleOddNumbers which accepts an array and returns a new array with all of the odd numbers doubled
232+
(HINT - you can use map and filter to double and then filter the odd numbers).
233+
234+
Examples:
235+
doubleOddNumbers([1,2,3,4,5]) // [2,6,10]
236+
doubleOddNumbers([4,4,4,4,4]) // []
237+
*/
238+
239+
function doubleOddNumbers(arr) {
240+
const oddNumbers = arr.filter(function(number) {
241+
return number % 2 !== 0;
242+
})
243+
return oddNumbers.map(function(num) {
244+
return num + num;
245+
})
246+
}

someEvery.js

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
Write a function called hasOddNumber which accepts an array and returns true if the array contains
3+
at least one odd number, otherwise it returns false.
4+
5+
Examples:
6+
hasOddNumber([1,2,2,2,2,2,4]) // true
7+
hasOddNumber([2,2,2,2,2,4]) // false
8+
*/
9+
10+
function hasOddNumber(arr) {
11+
return arr.some(function (val) {
12+
if (val % 2 !== 0) {
13+
return true}
14+
return false;
15+
})
16+
}
17+
18+
/*
19+
Write a function called hasAZero which accepts a number and returns true if that number contains at least one zero.
20+
Otherwise, the function should return false
21+
22+
Examples:
23+
hasAZero(3332123213101232321) // true
24+
hasAZero(1212121) // false
25+
*/
26+
27+
function hasAZero(num) {
28+
const numArr = num.toString().split('');
29+
return numArr.some(function(val) {
30+
return val === '0';
31+
})
32+
}
33+
34+
35+
/*
36+
Write a function called hasOnlyOddNumbers which accepts an array and returns true if every single number
37+
in the array is odd. If any of the values in the array are not odd, the function should return false.
38+
39+
Examples:
40+
hasOnlyOddNumbers([1,3,5,7]) // true
41+
hasOnlyOddNumbers([1,2,3,5,7]) // false
42+
*/
43+
44+
function hasOnlyOddNumbers(arr) {
45+
return arr.every(function(num) {
46+
return num % 2 !== 0;
47+
})
48+
}
49+
50+
/*
51+
Write a function called hasNoDuplicates which accepts an array and returns true if there are no duplicate values
52+
(more than one element in the array that has the same value as another).
53+
If there are any duplicates, the function should return false.
54+
55+
Examples:
56+
hasNoDuplicates([1,2,3,1]) // false
57+
hasNoDuplicates([1,2,3]) // true
58+
*/
59+
60+
function hasNoDuplicates(arr) {
61+
return arr.every(function(num) {
62+
if(arr.indexOf(num) === arr.lastIndexOf(num)) {
63+
return true;
64+
};
65+
return false;
66+
})
67+
}
68+
69+
/*
70+
Write a function called hasCertainKey which accepts an array of objects and a key,
71+
and returns true if every single object in the array contains that key. Otherwise it should return false.
72+
73+
Examples:
74+
var arr = [
75+
{title: "Instructor", first: 'Elie', last:"Schoppik"},
76+
{title: "Instructor", first: 'Tim', last:"Garcia", isCatOwner: true},
77+
{title: "Instructor", first: 'Matt', last:"Lane"},
78+
{title: "Instructor", first: 'Colt', last:"Steele", isCatOwner: true}
79+
]
80+
81+
hasCertainKey(arr,'first') // true
82+
hasCertainKey(arr,'isCatOwner') // false
83+
*/
84+
85+
function hasCertainKey(arr, key) {
86+
return arr.every(function(obj) {
87+
return obj[key];
88+
})
89+
}
90+
91+
/*
92+
Write a function called hasCertainValue which accepts an array of objects and a key, and a value,
93+
and returns true if every single object in the array contains that value for the specific key.
94+
Otherwise it should return false.
95+
96+
Examples:
97+
var arr = [
98+
{title: "Instructor", first: 'Elie', last:"Schoppik"},
99+
{title: "Instructor", first: 'Tim', last:"Garcia", isCatOwner: true},
100+
{title: "Instructor", first: 'Matt', last:"Lane"},
101+
{title: "Instructor", first: 'Colt', last:"Steele", isCatOwner: true}
102+
]
103+
104+
hasCertainValue(arr,'title','Instructor') // true
105+
hasCertainValue(arr,'first','Elie') // false
106+
107+
*/
108+
109+
function hasCertainValue(arr, key, searchValue) {
110+
return arr.every(function(obj) {
111+
return obj[key] === searchValue;
112+
})
113+
}

0 commit comments

Comments
 (0)