15,794,275 members
1.00/5 (1 vote)
See more:
Write a function called crazySum that takes an array of numbers, and returns the sum of each number multiplied by its position in the array.

For example, if you pass [4, 8, 15, 16], the program will calculate 4*1 + 8*2 + 15*3 + 16*4 and give you that back as an answer.
I am attempting to work through this by taking an x (value) and i (placement) and then using *+ to find the sum, but can't extract more than just the value.
Here is the beginning of my solution:

JavaScript
```var array = [3,2,2];
crazySum = function () {
i=1
for(var i = 1; i<array.length; i++) {
return i
}
}
console.log(crazySum[i]+1);```

Any help or links to similar queries is very much appreciated =)
Posted
Updated 25-Dec-15 9:24am
v3
PIEBALDconsult 25-Dec-15 14:24pm
You seem to be returning i too soon.
littlecodebaby 27-Dec-15 14:39pm
Thanks for reading my question @PIEBALD, my intention with returning i then was just to check what the function was doing, thinking that breaking the steps down a little would give me a chance to find errors and troubleshoot.
I see that you're very active and experienced on this site, and want to ask a favour. if you can think of any examples of functions that take arrays, could you pass me the links? I hope with exposure to more examples of similar things, I can manipulate the parts to run my function. Thank you!

ps coded thus v the function returns "undefined."
var array = [];
crazySum = function ([]) {
var sum= 0;
for (var i= 0; i< array.length;i++)
{
sum += ((i+1) * array[i]);
}
};

console.log(crazySum([2,3,2]))
Mohibur Rashid 25-Dec-15 20:00pm
Have you learned javascript? Or did you do any simple program that demonstrate hoe to get sum of array?
[no name] 26-Dec-15 8:17am
Please note first, I'm not familar with Java, so take care with my comment/syntax.
a.) I'm missing the sum you like to calculate/return
b.) Your for Loop seems to miss the first element.
var sum= 0;
for (var i= 0; i< array.length;i++)
{
sum= sum + (i+1) * Array[i];
}

// At this point, sum should have the value you excpect.
Do you see the differences?
littlecodebaby 27-Dec-15 12:55pm
Thank you for trying something =)
I built your code into my function but is still returning "undefined." I will let you know when I work it out (with slight changes, capitalization etc.).

ref:
var array = [];
crazySum = function () {
var sum= 0;
for (var i= 0; i< array.length;i++)
{
sum += (i+1) * array[i];
}
};

crazySum([2,3,2])

## Solution 1

Doesn't matter what you do the function will only ever return a value of 1. I would suggest going to JavaScript Tutorial[^] and working through the tutorials.

## Solution 2

Finally decided to post my comment as a solution.

Please note first, I'm not familar with Java, so take care with my comment/syntax.
a.) I'm missing the sum you like to calculate/return
b.) Your for Loop seems to miss the first element.
JavaScript
```var sum= 0;
for (var i= 0; i< array.length;i++)
{
sum= sum + (i+1) * Array[i];
}```

// At this point, sum should have the value you excpect.
Do you see the differences?