user127774
user127774

Reputation: 1

loops in JavaScript

I know exactly how to write both for loops and while loops, however I struggle with what comes after. For example, I did an interactive tutorial and was asked to write a for loop that adds numbers 1 through 10.

This is the syntax for the for loop:

{ 
    var currentSum = 0;
    for (var i=1; i<= num; i++) {
        currentSum += i; // <--
    }
    return current sum  // <--
}
console.log(sum(10))

I understand what is in regular writing, and I marked what I don't understand. I know what they mean, I just don't know why I was supposed to put them as opposed to something else and I don't get how I was supposed to know that those are the right things to code.

Thanks in advance for your help!

Upvotes: 0

Views: 112

Answers (3)

Janak
Janak

Reputation: 5052

function sum(n)
{ 
    var currentSum = 0;
    for (var i=1; i<=num; i++) {
        currentSum += i; // <-- 1*
    }
    return currentSum; // <-- 2*     
}

console.log(sum(10));

1* --> Short hand operator for addition. You can use either this or

   currentSum  = currentSum  + i

2* --> Seems to be a little incorrect here. The space should not be there. you are returning the computed value back from the function.

Upvotes: 0

OJay
OJay

Reputation: 4921

With annotations, does this help?

function sum(num) <-- you seem to be missing this
{ 
    var currentSum = 0;           // <-- declare variable and assign it to 0
    for (var i=1; i<= num; i++) { // <--start a loop, with i as the counter, 
                                  //    starting at 1 and finishing at whatever 
                                  //    the value of num is
        currentSum += i;    // <-- take the current value of currentSum and 
                            //     add i to it, shorthand way of writing 
                            //     currentSum = currentSum + i
    }
    //return current sum  // <-- This line is completely wrong
    return currentSum;    // <-- Return the variable currentSum after all the 
                          //       looping and summing has finished

}
//consol.long(sum(10));    // <-- this line is also wrong, there is no such 
                           //     thing as consol.long
console.log(sum(10));      // <-- should be console.log, which is outputting 
                           //     the return value of the function call sum, 
                           //     passing in a value of 10 as the input 
                           //     parameter num, to the console.
                           //     Which in most browsers, can be located via F12

Upvotes: 1

Priyank
Priyank

Reputation: 1328

1) currentSum += i; // <--

Refer to JavaScript Assignment Operators. It can also be written as

currentSum = currentSum + i;

so this will store its previous value and keeping add new ones.

2) return current sum // <--

I feel this is invalid. It should return

 return currentSum  // will return the final value.

Upvotes: 1

Related Questions