7.return keyword


#1

cant figur out what is wrong with this . help pls?

var timesTwo = function(number) {
return number * 2;
};

timesTwo(8);
var newNumber = function (timesTwo){
console.log(newNumber);
};


#2

You can make that code much more smalerl and will do the same job just replace return with console.log();


#3

this is the code you start with:

// Parameter is a number, and we do math with that parameter
var timesTwo = function(number) {
    return number * 2;
};

// Call timesTwo here!
var newNumber = 
console.log(newNumber);

the only thing you need to do, is call the function on line 7, that is it. You seemed to have made more changes


#4

i dont know what im doing wrong!! help?

// Parameter is a number, and we do math with that parameter
var timesTwo = function(number) {
return number * 2;
};

// Call timesTwo here!
var newNumber = timesTwo(8)
console.log = ("newNumber");

Oops, try again. It looks like you didn't print out the value of newNumber


#5

see the equals sign after newNumber? that means that newNumber is assigned to the function of timesTwo()

so basically newNumber is now the result of the function timesTwo(8), all you have to do now is print out newNumber and it will show the answer to timesTwo(8). you don't use quotation marks or an equals sign, just print it out how you have printed out every non-text item up to this point.


#6

I'd rather say that the result of timesTwo(8) gets assigned to newNumber but it's obvious that you mean the right thing.

@ernesto2013
Also in addition to @fonzihippo answer after you fixed your code also refresh the page because by:

console.log = ("newNumber");

you reassigned console.log to be "newNumber" but you want it to be the function that prints to the console.


#7

does not work on mine


#8

you don't put function word just name of the function!

Like here:
var newNumber = timesTwo(8);


#9

nevermind it worked. The computer itself had an error


#10

Try do this in safari, like this:
var newNumber = timesTwo(8)
console.log(newNumber);


#12

I was able to get this lesson right, but I'm still having trouble understanding the concept of the return keyword. I understand that return keeps the value without printing the value.

What I don't understand is why this works:

ORIGINAL CODE

// Parameter is a number, and we do math with that parameter
var timesTwo = function(number) {
return number * 2;
};

// Call timesTwo here!
var newNumber = timesTwo(2);
console.log(newNumber);

But not this:

// Parameter is a number, and we do math with that parameter
var timesTwo = function(number) {
number * 2;
};

// Call timesTwo here!
var newNumber = timesTwo(2);
console.log(newNumber);

Is it because not having return or console.log within the function an incorrect syntax for writing functions?


#13

you understand that using console.log will log something to the console directly? example:

var timesTwo = function(number) {
   console.log(number * 2);
};
timesTwo(2);

however, return returns you something, it gives you something in return. If i give you a equation to solve on a piece of paper (2*2 for example), you fill in 4, and give me back the piece of paper. Now i have a new value (4), which i can store in a variable:

var newNumber = timesTwo(2);

this line is crucial, the timeTwo function gets executed, and return 4. So after the function is executed you could say it looks like this:

var newNumber = 4

now newNumber is 4, and you can print that to the console:

console.log(newNumber);

#14

This is my code and I get an error: console.log is not a function
// Parameter is a number, and we do math with that parameter
var timesTwo = function(number) {
return number * 2;
};

// Call timesTwo here!
var newNumber = timesTwo(8);
console.log(newNumber);

What is wrong with my code?


#15

Your code works fine, seems your browser is sending cached data, try a different browser

Also, if you have more questions, create your own topic.