Sum should be equal to eight

Tell us what’s happening:

Your code so far


// Setup
var sum = 0;

function addThree() {
sum += 3;
}

// Only change code below this line
function addFive() {
var sum = 5;
sum += 5;
}


// Only change code above this line

addThree(5);
addFive(3);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36.

Challenge: Understanding Undefined Value returned from a Function

Link to the challenge:

doing this you are no more changing the global variable, so the global variable is equal to 3, and this local variable is equal to 10

I think this kind of topic you need to find the right representation. I’ll just give you a funny way to see it, afaik:

// Setup
var commonPot = 0; 

function addThreeForAll() {
commonPot += 3; // adds to the outer variable
}

// Only change code below this line
function addFiveforMe() {
var myPot = 5; //we creating a local pot
myPot += 5; // adding only to local pot
}

addThree( );  //no value needed, you already set 3
addFive( ); //no value needed, you already set 5

If you look at this, declaring a variable inside a function is like creating a local pot. Creating a variable outside of it, is a commonPot.

You’re assigning the value 5 to the variable sum and then adding 5 to it. That’s why sum becomes 10 after the operation. Simply exclude the line var sum = 5 and you should be good to go.

P.S.: Variables declared in JS with the keywords var and let are mutable, and that’s exactly why you can add five to it and also assign a new value to it as well. Hope this makes things clearer.