3.5 Array Helpers Part 2¶
every()¶
Task 1: Check if the elements in the array below are numbers. Hint: use map()
var arr = [5, 7, 0, 9, -88, 3, 6, 7];
var newArr = arr.map(value => typeof value === "number" ? 1 : 0);
console.log(newArr);
//You can also add
var count = 0;
for (element of newArr){
count += element;
}
var allNumbers = count === newArr.length ? true : false;
console.log(allNumbers);
The result is as follows:
(8) [1, 1, 1, 1, 1, 1, 1, 1]
true
Task 2: Follow the task 1 approach to find if all the following array elements are numbers or not.
var arr = [true, 5, 7, 0, null, 9, "value", -88, 3, 6, 7];
var newArr = arr.map(value => typeof value === "number" ? 1 : 0);
console.log(newArr);
//You can also add
var count = 0;
for (element of newArr){
count += element;
}
var allNumbers = count === newArr.length ? true : false;
console.log(allNumbers);
The result is as follows:
(11) [0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1]
false
Task 3: Check the following code, and compare it with the task 1.
var arr = [5, 7, 0, 9, -88, 3, 6, 7];
var allNumbers = arr.every(value => typeof value === "number");
console.log(allNumbers);
The result is: true
Task 4: Check the following code, and compare it with the task 2.
var arr = [true, 5, 7, 0, null, 9, "value", -88, 3, 6, 7];
var allNumbers = arr.every(value => typeof value === "number");
console.log(allNumbers);
The result is: false
every()
Using array.every()
, we can check if all the array element satisfy a certain condition.
array.every()
takes a function as an input, and it returns either true or false.
This input function accepts up to three arguments:
currentValue
: the current element being processed in the array. It is required.index
: the index of the current element. It is optional.array
: the array itself. It is optional.
Task 5: Check if all the following array elements are greater than 20 or not.
var arr = [10, 20, 5, 2, -8, 0, 55, 100];
var all = arr.every(value => value > 20);
console.log(all);
The result is: false
Task 6: Check if all the following array elements are less than 110.
var arr = [10, 20, 5, 2, -8, 0, 55, 100];
var all = arr.every(value => value < 110);
console.log(all);
The result is: true
Task 7: Check if all the following array elements are ones.
var arr = [1, 1, 1, 0, 0, 1, 1, 1];
var all = arr.every(value => value === 1);
console.log(all);
The result is: false
Task 8: Check if all the following array elements are zeros.
var arr = [0, 0, 0, 0, 0];
var all = arr.every(value => value === 0);
console.log(all);
The result is: true
Task 9: Check if the element index in the array below is equal to its value. Return true if the condition is satisfied for all array elements; otherwise, return false.
var arr = [0, 1, 2, 3, 4, 5];
var all = arr.every(function(value, index){
return value == index;
});
console.log(all);
The result is: true
Task 10: Check if the element index in the array below is equal to its value. Return true if the condition is satisfied for all array elements; otherwise, return false.
var arr = [0, 2, 2, 7, 4, 5];
var all = arr.every(function(value, index){
return value == index;
});
console.log(all);
The result is: false
Task 11: Check if all the strings in the following array has a length of 3.
var names = ["Sami", "Sanad", "Ali", "Nawras", "Jad", "Ola", "JoJo", "Ahmed", "Lolo"];
var all = names.every(name => name.length === 3);
console.log(all);
The result is: false
Task 12: Check if all the strings in the following array contain a
or A
character.
var names = ["Sami", "Sanad", "Ali", "Nawras", "Jad", "Ola", "Julia", "Ahmed", "Lala"];
var all = names.every(name => name.toLowerCase().indexOf('a') !== -1 );
console.log(all);
The result is: true
Task 13: Check if all the users are logged in.
var usersLoggedInArr = [true, false, true, true, true, false];
var all = usersLoggedInArr.every(value => value === true);
console.log(all);
The result is: false
some()¶
Task 14: Check if any element of the array below is a number. Hint: use map()
var arr = [5, 7, 0, 9, -88, 3, 6, 7];
var newArr = arr.map(value => typeof value === "number" ? 1 : 0);
console.log(newArr);
//You can also add
var count = 0;
for (element of newArr){
count += element;
}
var anyIsNumber = count > 0 ? true : false;
console.log(anyIsNumber);
The result is as follows:
(8) [1, 1, 1, 1, 1, 1, 1, 1]
true
Task 15: Check if any element of the array below is a number. Hint: use map()
var arr = [true, 5, 7, 0, null, 9, "value", -88, 3, 6, 7];
var newArr = arr.map(value => typeof value === "number" ? 1 : 0);
console.log(newArr);
//You can also add
var count = 0;
for (element of newArr){
count += element;
}
var anyIsNumber = count > 0 ? true : false;
console.log(anyIsNumber);
The result is as follows:
(11) [0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1]
true
Task 16: Check if any element of the array below is a number. Hint: use map()
var arr = ["hello", "Ali", false, null];
var newArr = arr.map(value => typeof value === "number" ? 1 : 0);
console.log(newArr);
//You can also add
var count = 0;
for (element of newArr){
count += element;
}
var anyIsNumber = count > 0 ? true : false;
console.log(anyIsNumber);
The result is as follows:
(4) [0, 0, 0, 0]
false
Task 17: Check the following code, and compare it with the task 14.
var arr = [5, 7, 0, 9, -88, 3, 6, 7];
var anyIsNumber = arr.some(value => typeof value === "number");
console.log(anyIsNumber);
The result is: true
Task 18: Check the following code, and compare it with the task 15.
var arr = [true, 5, 7, 0, null, 9, "value", -88, 3, 6, 7];
var anyIsNumber = arr.some(value => typeof value === "number");
console.log(anyIsNumber);
The result is: true
Task 19: Check the following code, and compare it with the task 16.
var arr = ["hello", "Ali", false, null];
var anyIsNumber = arr.some(value => typeof value === "number");
console.log(anyIsNumber);
The result is: false
some()
Using array.some()
, we can check if at least one element of an array satisfies a certain condition.
array.some()
takes a function as an input, and it returns either true or false.
This input function accepts up to three arguments:
currentValue
: the current element being processed in the array. It is required.index
: the index of the current element. It is optional.array
: the array itself. It is optional.
Task 20: Check if any of the following array elements is greater than 20 or not.
var arr = [10, 20, 5, 2, -8, 0, 55, 100];
var someElements = arr.some(value => value > 20);
console.log(someElements);
The result is: true
Task 21: Check if any of the following array elements is less than 110.
var arr = [10, 20, 5, 2, -8, 0, 55, 100];
var someElements = arr.some(value => value < 110);
console.log(someElements);
The result is: true
Task 22: Check if the element index in the array below is equal to its value. Return true if the condition is satisfied for some array elements; otherwise, return false.
var arr = [0, 2, 2, 7, 4, 5];
var someElements = arr.some(function(value, index){
return value == index;
});
console.log(someElements);
The result is: true
Task 23: Check if any of the strings in the following array has a length of 3.
var names = ["Sami", "Sanad", "Nawras", "JoJo", "Ahmed", "Lolo"];
var someElements = names.some(name => name.length === 3);
console.log(someElements);
The result is: false
Task 24: Check if any of the strings in the following array contains z
character.
var values = ["Hello", "well", "can we do it?", "now better than never"];
var someElements = values.every(value => value.indexOf('z') !== -1 );
console.log(someElements);
The result is: false
Task 25: Check if any of the users is logged in.
var usersLoggedInArr = [true, false, true, true, true, false];
var someElements = usersLoggedInArr.some(value => value === true);
console.log(someElements);
The result is: true