-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproduct-array.js
More file actions
73 lines (57 loc) · 2.28 KB
/
product-array.js
File metadata and controls
73 lines (57 loc) · 2.28 KB
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
71
72
73
/*
Introduction and Warm-up (Highly recommended)
Playing With Lists/Arrays Series
Task
Given an array/list [] of integers , Construct a product array Of same size Such That prod[i] is equal to The Product of all the elements of Arr[] except Arr[i].
Notes
Array/list size is at least 2 .
Array/list's numbers Will be only Positives
Repetition of numbers in the array/list could occur.
Input >> Output Examples
productArray ({12,20}) ==> return {20,12}
Explanation:
The first element in prod [] array 12 is the product of all array's elements except the first element
The second element 20 is the product of all array's elements except the second element .
productArray ({1,5,2}) ==> return {10,2,5}
Explanation:
The first element 10 is the product of all array's elements except the first element 1
The second element 2 is the product of all array's elements except the second element 5
The Third element 5 is the product of all array's elements except the Third element 2.
productArray ({10,3,5,6,2}) return ==> {180,600,360,300,900}
Explanation:
The first element 180 is the product of all array's elements except the first element 10
The second element 600 is the product of all array's elements except the second element 3
The Third element 360 is the product of all array's elements except the third element 5
The Fourth element 300 is the product of all array's elements except the fourth element 6
Finally ,The Fifth element 900 is the product of all array's elements except the fifth element 2
A more challenging version of this kata by Firefly2002
Playing with Numbers Series
Playing With Lists/Arrays Series
For More Enjoyable Katas
ALL translations are welcomed
Enjoy Learning !!
Zizou
*/
function productArray(numbers){
let initialValue = 0
let sumWithInitial = 0
let arrayReturn = []
for(let i = 0; i < numbers.length; i++){
//console.log(i)
sumWithInitial = 0
sumWithInitial = numbers.reduce((previousValue, currentValue, index) => {
//console.log(index)
//console.log(i)
//console.log(currentValue)
//console.log(previousValue)
if (index != i){
return previousValue*currentValue
} else {
return previousValue
}
}, 1)
//console.log(sumWithInitial)
arrayReturn.push(sumWithInitial)
}
return arrayReturn
}