-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiterators-every.js
41 lines (30 loc) · 1.23 KB
/
iterators-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
// Iterators .every() Method
// The every() method tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value.
/////////////////////////////////////////
// Ex 1
const words = ['unique', 'uncanny', 'pique', 'oxymoron', 'guise'];
console.log('======= ex 1 =======');
console.log(words.every((word) => {
return word.length > 5;
} )); // false
/////////////////////////////////////////
// Ex 2
function sourcePlant (food) {
if(food.source === 'plant') {
return true;
}
return false;
}
const isTheDinnerVegan = arr => {
if(arr.every(sourcePlant)) {
return true;
} else {
return false;
}
}
const dinner = [{name: 'hamburger', source: 'meat'}, {name: 'cheese', source: 'dairy'}, {name: 'ketchup', source:'plant'}, {name: 'bun', source: 'plant'}, {name: 'dessert twinkies', source:'unknown'}];
// veganDinner returns true
const veganDinner = [{name: 'hamburger', source: 'plant'}, {name: 'cheese', source: 'plant'}, {name: 'ketchup', source:'plant'}, {name: 'bun', source: 'plant'}, {name: 'dessert twinkies', source:'plant'}];
console.log('======= ex 2 =======');
console.log(isTheDinnerVegan(dinner)) // false
console.log(isTheDinnerVegan(veganDinner)) // true