-2

sorry i new learning about javascript: i have code like this:

function call(arr) {
 //i have no idea 
}
console.log(call([1,2,3]));

i want to output:

//[6,3,2]

where result output obtained from [(2*3),(1*3),(1*2)]

justunknown
  • 55
  • 1
  • 6
  • 1
    Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. – Zakaria Acharki Feb 01 '17 at 19:12
  • What you're looking for may be described as manipulating the permutation of an array. Take a look at http://stackoverflow.com/questions/9960908/permutations-in-javascript/37580979#37580979 for an in-depth discussion. – Snowmonkey Feb 01 '17 at 19:14

1 Answers1

1

It looks like you want each value in the array to be replaced with the value of all of the other elements multiplied together. You can calculate this more efficiently by first multiplying all of the numbers together, and then using division to take away just one number from the product at any one time, leaving the product of all the other numbers in the array.

Array.prototype.reduce Array.prototype.map

function call(arr) {
  var product = arr.reduce((a,b) => a * b, 1);
  return arr.map(num => product / num);
}

console.log(call([1, 2, 3]));

Note that this won't work if a 0 appears anywhere in the array.

4castle
  • 32,613
  • 11
  • 69
  • 106