0

Ok, this might be easy for some genius out there but I'm struggling...

This is for a project I'm working on with a slider, I want an array the slider can use for snap points/increments... I'm probably going about this in a mental way but its all good practice! Please help.

var frootVals = [1,2,3,4,5];
var frootInc = [];

    for (i=0; i<=frootVals.length; i++) {
            if (i == 0){
            frootInc.push(frootVals[i]);
            }
            else{
            frootInc.push(frootInc[i-1] += frootVals[i])
            }
        };

What I'm trying to do is create the new array so that its values are totals of the array elements in frootVals.

The result I'm looking for would be this:

fruitInc = [1,3,6,10,15]
2
  • Change += to + and <= to <. You can avoid the awkward if statement by initializing the array with var frootInc = [frootVals[0]]; and changing the initialization of the loop variable to i=1. Commented Sep 23, 2014 at 1:07
  • I like what you did there. Cool, thanks man. Commented Sep 23, 2014 at 1:12

5 Answers 5

1

For a different take, I like the functional approach:

var frootVals = [1,2,3,4,5];
var frootInc = [];
var acc = 0;
frootVals.forEach(function(i) {
    acc = acc + i;
    frootInc.push(acc);
});
Sign up to request clarification or add additional context in comments.

Comments

0
var frootVals = [1,2,3,4,5]
  , frootInc = [];

// while i < length, <= will give us NaN for last iteration
for ( i = 0; i < frootVals.length; i++) {
    if (i == 0) {
        frootInc.push(frootVals[i]);
    } else {
        // rather than frootIne[ i-1 ] += ,
        // we will just add both integers and push the value
        frootInc.push( frootInc[ i-1 ] + frootVals[ i ] )
    }
 };

There were a few things wrong with your code check out the commenting in my code example. Hope it helps,

Comments

0

This will do:

var frootVals = [1,2,3,4,5];
var frootInc = [];

for (i=0; i < frootVals.length; i++) { // inferior to the length of the array to avoid iterating 6 times
    if (i == 0) {
        frootInc.push(frootVals[i]);
    }
    else {
        frootInc.push(frootInc[i-1] + frootVals[i]) // we add the value, we don't reassign values
    }
};

alert(JSON.stringify(frootInc));

jsfiddle here: http://jsfiddle.net/f01yceo4/

2 Comments

Banging, thanks for the quick response mate :) ALSO - how comes you use JSON.stringify - if you just did the alert(frootInc) would it not show the elements properly?? because that's what I've been using.
It's an habit. I think it's ok to use alert. You can also use console.log() and check it in the console (with the inspector of your browser). developer.chrome.com/devtools/docs/console
0

change your code to:

var frootVals = [1,2,3,4,5];
var frootInc = [frootvals[0]]; //array with first item of 'frootVals' array

for (i=1; i<frootVals.length; i++) {
    frootInc.push(frootInc[i-1] + frootVals[i]); //remove '='
}

Comments

0

Here's a very simple pure functional approach (no vars, side-effects, or closures needed):

[1,2,3,4,5].map(function(a){return this[0]+=a;}, [0]);
// == [1, 3, 6, 10, 15]

if you name and un-sandwich the function, you can use it over and over again, unlike a hard-coded var name, property name, or for-loop...

2 Comments

this is good (and you get my +1) but i think using a this object with more obvious intent is better as an example, like this maybe: var a = [1,2,3,4,5].map(function(a) {return this.acc += a;}, {acc: 0} );
@Segfault: fair 'nuf... the important point in my answer is keeping the function pure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.