-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsome-every.js
108 lines (88 loc) · 3.27 KB
/
some-every.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
Write a function called hasOddNumber which accepts an array and returns true if the array contains at least one odd number, otherwise it returns false.
Examples:
hasOddNumber([1,2,2,2,2,2,4]) // true
hasOddNumber([2,2,2,2,2,4]) // false
*/
function hasOddNumber(arr) {
return arr.some(function(val) {
if(val % 2 !== 0) {
return true;
} return false;
});
}
/*
Write a function called hasAZero which accepts a number and returns true if that number contains at least one zero. Otherwise, the function should return false
Examples:
hasAZero(3332123213101232321) // true
hasAZero(1212121) // false
*/
function hasAZero(num) {
return num.toString().split('').some(function(val){
if(val == 0){
return true;
} return false;
})
}
/*
Write a function called hasOnlyOddNumbers which accepts an array and returns true if every single number in the array is odd. If any of the values in the array are not odd, the function should return false.
Examples:
hasOnlyOddNumbers([1,3,5,7]) // true
hasOnlyOddNumbers([1,2,3,5,7]) // false
*/
function hasOnlyOddNumbers(arr) {
return arr.every(function(val){
if (val % 2 !== 0){
return true;
} return false;
})
}
/*
Write a function called hasNoDuplicates which accepts an array and returns true if there are no duplicate values (more than one element in the array that has the same value as another). If there are any duplicates, the function should return false.
Examples:
hasNoDuplicates([1,2,3,1]) // false
hasNoDuplicates([1,2,3]) // true
*/
function hasNoDuplicates(arr) {
return arr.every(function(val){
return arr.indexOf(val) === arr.lastIndexOf(val);
})
}
/*
Write a function called hasCertainKey which accepts an array of objects and a key, and returns true if every single object in the array contains that key. Otherwise it should return false.
Examples:
var arr = [
{title: "Instructor", first: 'Elie', last:"Schoppik"},
{title: "Instructor", first: 'Tim', last:"Garcia", isCatOwner: true},
{title: "Instructor", first: 'Matt', last:"Lane"},
{title: "Instructor", first: 'Colt', last:"Steele", isCatOwner: true}
]
hasCertainKey(arr,'first') // true
hasCertainKey(arr,'isCatOwner') // false
*/
function hasCertainKey(arr, key) {
return arr.every(function(val){
if(key in val){
return true;
}return false;
})
}
/*
Write a function called hasCertainValue which accepts an array of objects and a key, and a value, and returns true if every single object in the array contains that value for the specific key. Otherwise it should return false.
Examples:
var arr = [
{title: "Instructor", first: 'Elie', last:"Schoppik"},
{title: "Instructor", first: 'Tim', last:"Garcia", isCatOwner: true},
{title: "Instructor", first: 'Matt', last:"Lane"},
{title: "Instructor", first: 'Colt', last:"Steele", isCatOwner: true}
]
hasCertainValue(arr,'title','Instructor') // true
hasCertainValue(arr,'first','Elie') // false
*/
function hasCertainValue(arr, key, searchValue) {
return arr.every(function(val){
if (val[key] === searchValue){
return true;
} return false;
})
}