there is let arr = [val1, val2, val3, val4] I need the result of adding as an array for each val with each val1 + val2, val1 + val3 .... then val2 + val1, val2 + val3 .... and so on val is number

That's about the kind I want to get the result. Below is a pseudo-code. I did not have a working version to do

 let arr = [val1, val2, val3, val4].map((item) => { return { [val1 + val2, val1 + val3, val1 +val4], [val2 + val1, val2 + val3, val2 + val4], [val3 + val1, val3 + val2, val3 + val4], [val4 + val1, val4 + val2, val4 + val3] 

}})

  • need - do it. What exactly caused your problem? did not try the usual search? and where should the result be displayed? - Alex
  • ... then 4 + 3, 4+ 2 ... - 4+3, 4+4, 4+2, 4+5, 4+6 or 4+3, 4+2, 4+5, 4+6 ? - Alex
  • add the final result for the array given by you. So that you can see what you want to end up with - Grundy
  • Improved description, I hope it will be clearer! - pridan

1 answer 1

 let arr = [3, 4, 2, 5, 6]; arr.forEach(function(v, k) { let sum = v; arr.forEach(function(v2, k2) { sum += v2; }); console.log(sum); });