Understanding Undefined Value returned from a Function in JS

I m stuck for a long time with that need help with explaning

 **Your code so far**

// Setup
let sum = 3;

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

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

// Only change code above this line

addThree();
addFive();
 **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36

Challenge: Understanding Undefined Value returned from a Function

Link to the challenge:

This is explicitly not returning undefined…

The variable name, and the function parameter shall not be the same word.
The function as it’s written here will return 6, because “sum” has assigned 3 as a value to it.
and because you’re using the “return” keyword.

the goal of the challenge is to change the value of the variable, but return nothing as result from the function.
That is achieved if you don’t use return in your functions.
Also you do not need to use parameters for this challenge.

Your function doesn’t have an explicit return statement. What does a function in JS return if there is no return statement? You should be able to easily google the answer if you don’t know.

1 Like

i know isn’t needed it was trying to test something >>here what i had achieved i get 2 true point out of 4 from task completion points <<and here the notes of failure

1
Inside the addFive function, you should add 5 to the sum variable.

  1. Once both functions have run, the sum should be equal to 8 .

Your code so far


// Setup
let sum = 0 ;

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

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

// Only change code above this line

addThree();
addFive();
   **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36

Challenge: Understanding Undefined Value returned from a Function

Link to the challenge:

I’m not sure what you are saying?

Here is the original code:

// Setup
let sum = 0;

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

// Only change code below this line


// Only change code above this line

addThree();
addFive();

You change the function signature of addThree()! You made sum an argument which turns sum into a variable scoped locally to the function, but the challenge wants you to change the global variable sum.

1 Like

https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/understanding-undefined-value-returned-from-a-function

1 Like

Yes, that is the challenge that has been linked in this thread.

I did it Here is The right code >

// Setup

 let sum = 8 ;

function addThree(sum) {

  sum = sum + 3;

 

}

// Only change code below this line

function addFive (sum) { 

sum = sum + 5 ;

 

} 

// Only change code above this line

addThree();

addFive();
2 Likes

That shouldn’t pass…

Oh, its because you hard-coded the answer! You can’t do that!

You need to fix the issue I mentioned above where you were not supposed to use sum as an argument!

1 Like

Create the AddFive function with an empty argument. Use the + = operator to add the value 5 into the global variable.

I like a += b over a = a + b, but it is technically not required here and might not have been covered in the curriculum yet.

Edit: It was covered before this challenge. Still not required, but it certainly can be used.

Mod Edit: SOLUTION REDACTED
It was covered in the curriculum before and It passed this challenge

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.


Like I said, you can use it, but it is not required. I try not to tell people that they must do things that are not part of or needed for the challenge. It can lead to confusion.

I’ll sometimes comment on style and make “you should” comments though.

Sorry, i started now. I cancel the solution.

1 Like

The first and the second solutions are interchangeable. They have the same meaning.

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