|
1 | 1 | /**
|
2 | 2 | * Question 1
|
3 |
| - * Explain the concept of the every method in JavaScript arrays |
| 3 | + * Write a function that filters out all negative numbers from an array using the filter method. |
4 | 4 | */
|
5 | 5 |
|
6 |
| -// const numbers = [2, 4, 6, 8]; |
7 |
| -// const result = numbers.every((number) =>{ |
8 |
| -// return number % 2 == 0 |
9 |
| -// }) |
10 |
| - |
11 |
| -// console.log(result); |
12 |
| - |
| 6 | +// const myarr = [-10, 160, 87, 45, -120, 92, -73]; |
| 7 | +// const newMyArr = myarr.filter((num) => num >= 0); |
| 8 | +// console.log(newMyArr); |
13 | 9 | /**
|
14 | 10 | * Question 2
|
15 |
| - * Explain the concept of the flatMap method in JavaScript arrays. |
| 11 | + * Write a function using some to check if there is at least one element in an array that is a palindrome (reads the same backward as forward). |
16 | 12 | */
|
17 |
| -// const numbers = [2, 4, 6, 8, [1,[3]]]; |
18 |
| -// const newNum = numbers.flatMap(number => Array.isArray(number) ? number : number * 2) |
19 |
| -// // const newNums = numbers.flat() |
20 |
| -// // const newNum2 = newNums.map(number => number*2) |
21 |
| -// console.log(newNum); |
| 13 | + |
| 14 | +const stringArray = ["level", "hello", "racecar", "world"]; |
| 15 | + |
| 16 | +function hasPalindrom(word) { |
| 17 | + return word === word.split("").reverse().join(""); |
| 18 | +} |
| 19 | +const arrayIsPalindrom = stringArray.some((word) => hasPalindrom(word)); |
| 20 | +console.log(arrayIsPalindrom); |
22 | 21 |
|
23 | 22 | /**
|
24 | 23 | * Question 3
|
25 |
| - * Explain the concept of the Symbol.iterator in the context of array iteration. |
| 24 | + * Write a function that filter out on any criteria that user can provide in a callback function |
26 | 25 | */
|
27 |
| -// const numbers = [2, 4, 6, 8]; |
28 |
| -// const iterator = numbers[Symbol.iterator](); |
29 | 26 |
|
30 |
| -// while( ! next.done) { |
31 |
| -// console.log(next); |
32 |
| -// next = iterator.next(); |
33 |
| -// } |
| 27 | +// const filter = (arr, callback) => { |
| 28 | +// const filteredArr = []; |
| 29 | +// for (let i = 0; i < arr.length; i++) { |
| 30 | +// callback(arr[i]) ? filteredArr.push(arr[i]) : ""; |
| 31 | +// } |
| 32 | +// return filteredArr; |
| 33 | +// }; |
| 34 | + |
| 35 | +// const myarr = [-10, 160, 87, 45, -120, 92, -73]; |
| 36 | +// const newMyArr = filter(myarr, (num) => num % 2 == 0); |
| 37 | +// console.log(newMyArr); |
34 | 38 |
|
35 | 39 | /**
|
36 | 40 | * Question 4
|
37 |
| - * Explain the concept of the copyWithin method in JavaScript arrays. |
| 41 | + * Write a function customSome that replicates the functionality of the some method |
38 | 42 | */
|
39 |
| -// const numbers = [111, 2, 3, 4]; |
40 | 43 |
|
41 |
| -// numbers.copyWithin(3, 2) |
42 |
| -// console.log(numbers); |
| 44 | +// const customSome = (arr, callback) => { |
| 45 | +// for (let i = 0; i < arr.length; i++) { |
| 46 | +// if (callback(arr[i])) { |
| 47 | +// return true; |
| 48 | +// } |
| 49 | +// } |
| 50 | + |
| 51 | +// return false; |
| 52 | +// }; |
| 53 | + |
| 54 | +// const arrOfNumbers = [1, 3, 5, 7, 9]; |
| 55 | +// const isEven = customSome(arrOfNumbers, (num) => num % 2 === 0); |
| 56 | +// console.log(isEven); |
43 | 57 |
|
44 | 58 | /**
|
45 | 59 | * Question 5
|
46 |
| - * How can you reverse the order of elements in an array using array iteration methods? |
| 60 | + * Write a function customEvery that replicates the functionality of the every method |
47 | 61 | */
|
48 |
| - |
49 |
| -// const numbers = [1, 2, 3, 4]; |
50 |
| -// // numbers.reverse() |
51 |
| -// // console.log(numbers); |
52 |
| - |
53 |
| -// const reversedArray = numbers.map((_, index, array) => { |
54 |
| -// return array[array.length - 1 - index] |
55 |
| -// }) |
56 |
| -// console.log(reversedArray); |
|
0 commit comments