javascript - Multiply Array Elements by Index Position - Stack Overflow

I want to take the elements of an array, multiply them by their index position, and add the results tog

I want to take the elements of an array, multiply them by their index position, and add the results together.

So if I had:

array = [1, 2, 3] 

I would get 8.

// (1 * 0)+(2 * 1)+(3 * 2) = 8

What is the best way to do this in vanilla JS, simply for learning purposes?

I want to take the elements of an array, multiply them by their index position, and add the results together.

So if I had:

array = [1, 2, 3] 

I would get 8.

// (1 * 0)+(2 * 1)+(3 * 2) = 8

What is the best way to do this in vanilla JS, simply for learning purposes?

Share Improve this question edited Nov 16, 2016 at 20:13 Blorgbeard 104k50 gold badges235 silver badges276 bronze badges asked Nov 16, 2016 at 20:12 cj_bcj_b 131 silver badge3 bronze badges 4
  • 5 For learning purposes, you should probably try doing it yourself. Start with a for-loop. – Blorgbeard Commented Nov 16, 2016 at 20:14
  • 1 var t = 0, i = array.length; while(i--) t+= i * array[i]; – I wrestled a bear once. Commented Nov 16, 2016 at 20:17
  • 1 [1, 2, 3].reduce((s, n, i) => s + (n * i), 0); – Yoshi Commented Nov 16, 2016 at 20:18
  • Thanks you so much guys! My post for someone didn't include the part where I was frustrated over this for hours... but this morning I figured it out, using two for-loops, in the most convoluted was possible. But it works. So thank you for showing me the best way to do it! – cj_b Commented Nov 17, 2016 at 15:47
Add a ment  | 

6 Answers 6

Reset to default 4

A very simple way would be:

console.log(
                 // ┌ the running total
                 // │  ┌ the current array value
                 // │  │  ┌ the current array index
                 // │  │  │
  [1, 2, 3].reduce((s, n, i) => s + (n * i), 0)
);

Manual: Array.prototype.reduce()

A functional, semantic approach would be:

const add      = (a, b) => a + b;
const multiply = (a, b) => a * b;

To sum an array:

const sum      = a => a.reduce(add, 0);

To add up the products of value and index as you want:

const weightedSum = a => sum(a.map(multiply));

We're able to say a.map(multiply) because map happens to pass the value and the index to the callback multiply.

Of course, this might be more than you want or need to learn at this point. There's nothing wrong with an old-fashioned for loop, as others have mentioned:

function weightedSum(a) {
  sum = 0;
  for (let i = 0; i < a.length; i++) sum += a[i] * i;
  return sum;
}

You can use a map to multiply by index and then use a reduce to obtain the sum

var array = [1, 2, 3]
var result = array.map((x,i) => x*i).reduce((a,b) => a+b,0);
console.log(result);

several approaches, but simplest for you to understand would be:

var numbers = [1,2,3]; 
var sum = 0; 
for(var i = 0;i < numbers.length; i++){
    sum+= (i*numbers[i]);
}
console.log(sum);
var array = [1, 2, 3];
var items = array.map(function(x,i){
    return x * i;
});
var sum = items.reduce(function(init, x) {
    return x + init,0
});// 8
var result = 0;
for(i=0;i<array.length;i++)
{
    var val = array[i];
    result += val*i;
}
alert(result);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742271009a4412698.html

相关推荐

  • javascript - Multiply Array Elements by Index Position - Stack Overflow

    I want to take the elements of an array, multiply them by their index position, and add the results tog

    1天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信