-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsome-every.js
91 lines (72 loc) · 2.96 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
/*
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.
*/
function hasOddNumber(arr) {
return arr.some(function (val) {
return val % 2 !== 0;
});
}
hasOddNumber([1, 2, 2, 2, 2, 2, 4]) // true
hasOddNumber([2, 2, 2, 2, 2, 4]) // 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
*/
function hasAZero(num) {
return num.toString().split('').some(function (val) {
return val === '0';
})
}
hasAZero(3332123213101232321) // true
hasAZero(1212121) // 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.
*/
function hasOnlyOddNumbers(arr) {
return arr.every(function (val) {
return val % 2 !== 0;
});
}
hasOnlyOddNumbers([1, 3, 5, 7]) // true
hasOnlyOddNumbers([1, 2, 3, 5, 7]) // 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.
*/
function hasNoDuplicates(arr) {
return arr.every(function (val) {
return arr.indexOf(val) === arr.lastIndexOf(val)
});
}
hasNoDuplicates([1, 2, 3, 1]) // false
hasNoDuplicates([1, 2, 3]) // true
/*
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.
*/
function hasCertainKey(arr, key) {
return arr.every(function (val) {
return key in val
})
}
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
/*
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.
*/
function hasCertainValue(arr, key, searchValue) {
return arr.every(function (val) {
return val[key] === searchValue;
})
}
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