-
Notifications
You must be signed in to change notification settings - Fork 0
/
day05_3.js
53 lines (45 loc) · 1.1 KB
/
day05_3.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
/*
* Modify and return the array so that all even elements are doubled and all odd elements are tripled.
*
* Parameter(s):
* nums: An array of numbers.
*/
// function modifyArray(nums) {
// method - 1
// let newArray = [];
// for (let i = 0; i < nums.length; i++) {
// if (nums[i] % 2 == 0) {
// newArray[i] = nums[i] * 2;
// } else {
// newArray[i] = nums[i] * 3;
// }
// }
// return newArray;
// method - 2
// let newArray = nums.map(val => {
// if (val % 2 == 0) {
// return val * 2;
// } else {
// return val * 3;
// }
// })
// return newArray;
// method - 3
// let result = (n) => (n % 2 == 0 ? n * 2 : n * 3);
// let newArray = nums.map(result);
// return newArray;
// method - 4
// return nums.map(n => n = (n%2==0) ? n*2: n*3);
// }
// method - 5
// let modifyArray = (nums) => nums.map((val) => val * (2 + (val % 2)));
// method - 6
function modifyArray(nums) {
nums.forEach((element, index) => {
nums[index] = element * (2 + (element % 2));
});
return nums;
}
const n = 5;
const a = [1, 2, 3, 4, 5];
console.log(modifyArray(a).toString().split(",").join(" "));