Skip to content

Commit 8b8e03d

Browse files
committed
[main] 020 Practice JavaScript filter some every
1 parent 5147755 commit 8b8e03d

File tree

1 file changed

+40
-35
lines changed

1 file changed

+40
-35
lines changed

index.js

+40-35
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,61 @@
11
/**
22
* 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.
44
*/
55

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);
139
/**
1410
* 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).
1612
*/
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);
2221

2322
/**
2423
* 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
2625
*/
27-
// const numbers = [2, 4, 6, 8];
28-
// const iterator = numbers[Symbol.iterator]();
2926

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);
3438

3539
/**
3640
* 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
3842
*/
39-
// const numbers = [111, 2, 3, 4];
4043

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);
4357

4458
/**
4559
* 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
4761
*/
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

Comments
 (0)