Inside Output explainer

Aim of the puzzle: Declare a variable to use locally inside a function
Walk through of solution:
In some functions, it is necessary to declare a variable to store a value, and then return that variable at the end of the function.

In this puzzle, the sum() function will take an array of numbers, and return the sum of those numbers.

Inside the function, declare a variable to store the sum. Set its value to 0 to start.

Then, add a for loop to iterate through the array, and inside the code block, use the += operator to add each number in the array to the local variable.

At the end of the function, below the for loop, return the local variable.
Sample code solution:

let numbers = [2,3,5];
function sum(numberArray) {
    let sum = 0;
    for (let num of numberArray) {
        sum += num;
    }
    return sum;
}

console.log(sum(numbers));

Javascript Concepts: Variables, Functions, Output, For Loops, += Operator, Return Statements

1 Like

It won’t let me change var of num of number array to let like you have here and the code won’t run

When you create a for loop in the app, the var is automatically added and can’t (yet) be removed. I put let in the puzzle solution because that’s how the code would preferably be written, but you can use var and the puzzle will still complete.

Hope this helps!
Ben

1 Like

Yeah, the app won’t let you write it or they say that this solution is wrong. I have no idea what the communication problem is between the app and the solution board, but it’s there, and it’s very annoying

2 Likes

Yea same problem here. After try and try I revealed the solution, corrected a line and still won’t let me.run the program… I cannot advance at this time. :’-(

2 Likes


I don’t know what I’m doing wrong :persevere:

Hey there,

The function has the parameter numberArray. When the function is called with an argument like this:

sum(numbers)

or like this:

sum([1, 2, 3])

numberArray will store whatever value is placed in the parentheses.

Change your for loop so that it loops through numberArray, like this:

for (var element of numberArray)

Then, inside the for loop, you want to add each number to op, like this:

op += element

Finally, after all the numbers in numberArray have been looped through, return the value of op, like this:

return op

Hope this helps! Let me know if you have any other questions.
Ben

1 Like

I keep getting this message saying “starter code has been modified. Please reset,” but I think I minimally edited the code and think it is right, but please correct if not. If it is right, is there a bug in in this puzzle’s page? Thanks.!
Screenshot_20191205-172417|281x500

Yes you haven’t done anything wrong but you will have to stick with the original code

I feel like I did stick with the original code.

As another test, if I reset and only add “let out = 0;” and run, I already get the same error. Screenshot_20191206-194102|281x500

Instead of
let out = 0
Type
let sum = 0

I’m having the problem of returning my variable and yet I am still getting a message saying that I need to return it

return zc;

Should be outside the { } brackets of the for…loop

Thank you so much

Its working

Hey there, move your variable declaration so it’s inside the function.

I cant get this either. Please help! Everywhere I put the return sum it tells me to move it, amongst other things. I put it outside the block & it said to put after the for of loop. Same with sum +=num (add it,
move it…) Screenshot_20200122-104921|281x500

Hi! I’m confused not sure what to do

Hey there, it might help if you think about what the function does. The function takes an array of numbers, and returns the sum. For example, if you called the function like this: sum([2, 3, 2]), the function would return 7, because 2 + 3 + 2 = 7.

Inside the function, the sum variable stores the sum. To start, we set it to 0.

A for loop will loop through the numbers in the array and add each number to sum. Currently, your for loop just returns the sum variable, which means your function will always return 0. Change it to this:

for (var element of numberArray) {
    sum += element
}

Finally, return sum at the end of the function, after the for loop:

return sum

Hope that helps!
Ben

Hey there, you have return sum inside the for loop, which means that your loop will only look at the first number, add that number to sum, and then return the sum and stop executing.

Move the return statement down a line so it’s after the for loop, at the very end of the function.

I don’t understand where numberArray is coming from. Is it built into JS? Or is it declared in hidden code? I’m just starting to teach myself, and don’t really understand.