Return Value from Function with Return

Return Value from Function with Return
0
#1

Tell us what’s happening:

Your code so far


// Example
function minusSeven(num) {
  return num - 7;
}

// Only change code below this line
function timesFive() {
  return 5 * 5;

timesFive(2) 
  return 2 * 5;

timesFive(0) 
  return 0 * 5;
}

console.log(minusSeven(10));

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/return-a-value-from-a-function-with-return

0 Likes

#2

Your new timesFive function always returns 25. But what they want is for you to make it take in an argument or parameter which can be any number and then multiply it by 5.

Look back at the challenge explanation tolearn how to do that. Or just lookcarefully at the function above called minusSeven

0 Likes

#3

ok ill try that now thank you for the help

0 Likes

#4

it has come up with a syntax error under the fives at the top

0 Likes

#5

make sure your code is syntactically correct.

For eg. in the previous code you showed, you were missing a closing curly brace } after your function (and before the test calls).

Post your code here again, in between two lines with three back ticks like this:
```
```

0 Likes

#6

it’s not working. it only returns first value.

0 Likes

#7

That function doesn’t have variables in the body, only hardcoded numbers, so it will always returns the same thing. Also, return stop the function from running, everything after that is not considered.

It’s the difference between

function returnOne(num) {
   return 1;
}
returnOne(1);   // returns 1
returnOne(25);  // but also this returns 1

And this one

function returnNumber(num) {
  return num;
}

returnNumber(1);   // this returns 1
returnNumber(25);  // this returns 25

The second one uses variables to determinate what to return

0 Likes

#8

i still dont understand at all, how do i apply it to the challenge?

0 Likes

#9

Create a function timesFive that accepts one argument, multiplies it by 5 , and returns the new value. See the last line in the editor for an example of how you can test your timesFive function.

Create a function that takes a value and returns it multiplied by five. If you don’t use a variable it will always return the same number

0 Likes

#10

function minusSeven(num) {

return num - 7;

}

// Only change code below this line

function timesFive() {

return timesFive();

}

console.log(minusSeven(32));

console.log(minusSeven(17));

console.log(minusSeven(7));

//says max call stack exceeded !, i thought i had it

0 Likes

#11

I would suggest going back to the challenge, looking at the example and resetting the code. Be sure that you’re NOT hard coding the function, make sure your console.log is accurate. You’ll get it.

Great luck! - Dave

0 Likes

#12

You are calling the function from inside the function (don’t do that right now, you will learn about recursive functions later), you are not returning a number, and you are not using the parameter of the function to make the function return a different number everytime it is called with a different argument passed in

Look carefully at the example function, and read again the challenge description and requirements.

function functionName( /* here function parameters, if more than one they need to be separated by commas */  ) {
/* here function body, it should return something and use function parameter(s) */
}

// now let's call the function
functionName( /* here function arguments, for now make sure that they are the same number as parameters */ );

You can wrap the function call inside console.log() if you want to print what the function returns to the console

0 Likes

#13

I Finally Got It!!!

My Solution:

// Example

function minusSeven(num) {

return num - 7;

}

// Only change code below this line

function timesFive(num) {

return num * 5;

}

console.log(minusSeven(32));

console.log(minusSeven(17));

console.log(minusSeven(7));

1 Like