Basic JavaScript - Understanding Undefined Value returned from a Function

Tell us what’s happening:
Describe your issue in detail here.

Your code so far

// Setup
let sum = 0;

function addThree(num) {
  sum = num + 3;
}
// Only change code below this line
function addFive(num) {
  sum += 5;
}
var result = addFive(3); // This is undefined
// Only change code above this line

addThree(5);
addFive(3);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Understanding Undefined Value returned from a Function

Link to the challenge:

so in this code, I have a problem with getting an output of 8 where I have defined every thing please help.

Why are you giving both the functions parameters? The challenge starts out by not giving the addThree function a parameter, and you should not add any. Right now your code is doing this


function addThree(num) {
  sum = num + 3;
}

Your passing a parameter of “num” that is going to add 3 and equal the sum

function addFive(num) {
  sum += 5;
}

Here you are doing the same thing but adding 5 to whatever is equal to the sum at this point. Which is going to be whatever the sum equals after the addThree function completes.

Here is why this is a problem

addThree(5);

This function will make the sum equal 8 since you are passing in 5 and add in 3

addFive(3);

This is going to make the sum equal 13 because you are taking the value of sum which is 8 at this point, and you are adding 5 to it. Forget the 3 here because you are not even using it

As the directions say

" Create a function addFive without any arguments."

The addThree function should not have any arguments or parameters like the challenge started it out with, and the addFive function should not have any arguments or parameters. You can also get rid of the result variable you created, I am not sure why that is there.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.