What's going on here

Tell us what’s happening:
Hi

So I got this right but I think I need to clarify some points so I that I understand the why and if.

In my code I should use a return but I was playing around. When I takeaway addThree( )or addFive( ) I get undefined or nine.

So the value nine when I put 1 in the parameter if the first function gives me nine so both function are working.

Are addThree( ) and addFive( ) calling the function to run?

When I log either function I still get the value undefined if I log addThree or 9 if I use a return and addFive. Why are they logging different and c should I not just get the output of the value I log?

Thanks in advance.

Your code so far


// Setup
var sum = 1;

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

// Only change code below this line


// Only change code above this line

addThree();
addFive();

function addFive() {
return sum +5

}
console.log(addFive())

Your browser information:

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

Challenge: Understanding Undefined Value returned from a Function

Link to the challenge:

the challenge asks to create a function that changes sum
your addFive does not change sum

I think your questions are answered in the challenge description - every function that does not explicitly have a return statement returns undefined by default. The function can of course change a global variable like sum, but the returned value of the function is still undefined.

1 Like

Hi

Thanks for getting back. If I use a return function and log addFive() should it not return that function by itself?

We’re a little confused with terminology here… A function can either explicitly return a value with a return statement, or it implicitly returns undefined if there’s no such statement (a function can also return another function, but that’s going too far now).

There’s a difference between

  • returning a value from a function and
  • changing a global variable like sum

In the challenge, both functions addThree() and addFive() change the global variable. Now for what they return, it might help to assign the returned value to a variable test:

var sum = 3;

function addFive(){
  sum = sum + 5
}

var test1 = addFive()

console.log(test1) // undefined
console.log(sum) // 8

Now with how you modified the function. The function now returns 8, but the global variable wasn’t changed and is still 3:

function addFive(){
  return sum + 5
}

var test2 = addFive()

console.log(test2) // 8
console.log(sum) // 3

Now doing both, changing the global variable and also returning the result of the addition:

function addFive(){
  return sum = sum + 5
}

var test3 = addFive()

console.log(test3) // 8
console.log(sum) // 8

If this is confusing, don’t worry much about it. It’s one of the things that will just become clearer with time.

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