-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathproblem48.js
70 lines (46 loc) · 1.83 KB
/
problem48.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
## Practice Problem 47 ( map(), filter(), find() )
1. You have an odd array (array with odd numbers). [ 1, 3, 5, 7, 9 ].
Now convert this array into an even array (array with even
numbers). [ 2, 4, 6, 8, 10 ]. Do this using for loop & array.map()
method. Hints: add one to any odd number and it will become an even
number.
2. You are given an array say: [33, 50, 79, 78, 90, 101, 30 ]. Now
return/get all the elements which are divisible by 10 using
array.filter() method.
3. Now do the same task of question 2. But do this using array.find()
method. Then compare the output of question 2 & question 3.
prblem provider source : https://drive.google.com/file/d/1XcvIBe_rJlr6GY2rTnHlbIluTABXNhp7/view
*/
/* ===========================
Task One:
============================== */
const oddNumbers = [ 1, 3, 5, 7, 9 ] ;
const evenNumbersUsingLoop = [];
/* Using for loop */
for( var i = 0; i < oddNumbers.length; i++){
const number = oddNumbers[i];
const evenNumber = number + 1 ;
evenNumbersUsingLoop.push(evenNumber)
};
console.log(evenNumbersUsingLoop);
/* Using map() */
const evenNumbers = oddNumbers.map( number => number + 1);
console.log(evenNumbers);
/* ===========================
Task Two:
============================== */
const randomNumbers = [33, 50, 79, 78, 90, 101, 30 ] ;
const numbersDividedBy10 = randomNumbers.filter( number => number % 10 === 0);
console.log(numbersDividedBy10);
/* ===========================
Task Three:
============================== */
const firstNumberDividedBy10 = randomNumbers.find( number => number % 10 === 0);
console.log(firstNumberDividedBy10);
const compareTask2AndTask3 = `
The result of Task 2 is ${numbersDividedBy10}. (using filter method)
And
The result of Task 3 is ${firstNumberDividedBy10}. (using find method)
`;
console.log(compareTask2AndTask3);