forked from rithmschool/udemy_course_exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforEach-exercises.js
executable file
·51 lines (45 loc) · 977 Bytes
/
forEach-exercises.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// forEach
function doubleValues(arr){
var newArr = [];
arr.forEach(function(val){
newArr.push(val * 2)
})
return newArr;
}
function onlyEvenValues(arr){
var newArr = [];
arr.forEach(function(val){
if(val % 2 === 0){
newArr.push(val)
}
})
return newArr;
}
function showFirstAndLast(arr){
var newArr = [];
arr.forEach(function(val){
newArr.push(val[0] + val[val.length-1])
});
return newArr;
}
function addKeyAndValue(arr,key,value){
arr.forEach(function(val){
val[key] = value;
});
return arr;
}
function vowelCount(str){
var splitArr = str.toLowerCase().split("");
var obj = {};
var vowels = "aeiou";
splitArr.forEach(function(letter){
if(vowels.indexOf(letter) !== -1){
if(obj[letter]){
obj[letter]++;
} else{
obj[letter] = 1;
}
}
});
return obj;
}