-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHW 000000003
41 lines (36 loc) · 1.1 KB
/
HW 000000003
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
/**
* Returns the first number in an array that meets a condition.
* @param {array} arrayOfNum e.g. [3,4, 20, 333]
* @param {function} callback that receives a number and returns true or false
* @returns {number} in the array that is the result of the callback
*
* callback
* @param {number} num an item in an array
* @returns {boolean} if a number meets a condition
*
* @example
* const isNumberEven = (num) => {
* if (num % 2 === 0) return true;
* else return false;
* };
* const isNumberTwoDigits = (num) => {
* if (`${num}`.length === 2) {
* return true;
* } else return false;
* };
* console.log( findFirst([1, 3, 7, 8, 20], isNumberEven) ) // 8
* console.log( findFirst([4, 500, 30, 2], isNumberTwoDigits) ) // 30
*/
const findFirst = (arrayOfNum, callback) => {
// WRITE YOUR ANSWER HERE
};
// Solution 1: for loop
for (let i = 0; i < arrayOfNum.length; i++) {
if (callback(arrayOfNum[i])) return arrayOfNum[i];
}
// Solution 2: for ... of loop
for (let num of arrayOfNum) {
if (callback(num)) return num;
}
// IGNORE THIS BELOW. It is for the tests.
export { findFirst };