Hi there,
I don’t understand why I need to “call” the function before logging it into the console. Why do we call it and pass an argument to it, and then have to type all of it again when logging it into the console?
You probably wouldn’t want to print the function itself, you’d want its result instead.
You wouldn’t want to print addition but you might want to print the result of doing addition on 1 and 2
Logging is a one way operation. We send content to the method, and it outputs (logs) it to the console (the terminal window at right in the learning environment).
If we wish to output the result of our function running on the various arguments, we need that result before we can log it.
const hello = subject => `Hello ${subject}!`;
var result = hello('World');
console.log(result); // Hello World!
Alternatively, we can log the return value directly…
console.log(hello("Earth")) // Hello Earth!
Thanks for your responses, sorry but I’m really having a mental block on this.
In some cases, the exercises make me print the result by just calling the function (e.g. playGame() in the ‘Rock, Paper, Scissors’ exercise). But in some other cases, I need to log it into the console to get the result (e.g. “console.log(eightBall);” in the Magic Eight Ball exercise).
I’m not sure I understand when I can do what.
Usually a function shouldn’t print anything and instead return a value. Whoever calls the function can then choose what to do with that value (like displaying it)
Ok, let me take an example: in the “Paper, rock, scissors” exercise, I had to do the following:
const playGame = () => {
const userChoice =
getUserChoice('scissors');
const computerChoice =
getComputerChoice();
console.log('You threw: ' + userChoice);
console.log('The computer threw: ' + computerChoice);
console.log(determineWinner(userChoice, computerChoice));
};
playGame();
In the Magic Eight Ball exercise, my code is the following:
const randomNumber = Math.floor(Math.random() * 8);
let eightBall = '';
switch(randomNumber) {
case 0 :
eightBall = 'It is certain';
break;
case 1 :
eightBall = 'It is decidedly so';
break;
case 2 :
eightBall = 'Reply hazy try again';
break;
case 3 :
eightBall = 'Cannot predict now';
break;
case 4 :
eightBall = 'Do not count on it';
break;
case 5 :
eightBall = 'My sources say no';
break;
case 6 :
eightBall = 'Outlook not so good';
break;
case 7 :
eightBall = 'Signs point to yes';
break;
default :
eightBall = 'Invalid';
break;
}
console.log(eightBall);
Why don’t I need to do console.log(playGame)
in the first example?
Why can’t I just call eightBall()
in the second example?
If you have a piece of text then you can display it.
Are those both text?
Why don’t I need to do
console.log(playGame)
in the first example?
You don’t need to use the console.log command yourself because the playGame() function is calling three different console.log commands on it’s own.
Why can’t I just call
eightBall()
in the second example?
There is no function “eightBall()” created in that exercise, so calling it should do nothing.
In the exercise, you’re using a random number to change what string the eightBall variable is set to, and then calling the eightBall variable to the console to check that it’s actually changing.
Hey everyone! I’m a bit confused, why do I need to call a function right before logging the result to the console:
const plantNeedsWater = function(day) {
if(day === 'Wednesday') {
return true;
} else {
return false;
}
}
plantNeedsWater('Wednesday'); // here
console.log(plantNeedsWater('Wednesday'))
If I omit that step I will get the same result in the console as well. It seems that only the last value of the argument, which we have passed in here “console.log(plantNeedsWater(‘Wednesday’))”, matters. For example, if I assinged any other value there in the console.log step - I would get ‘false’, no matter what value we have assinged to the argument before. Thank you in advance.
who says you need to do this?
I was just doing everything according to task, one of the steps was to call the function and assign an argument. So as far as I understood there is no practical need to do this?
calling plantNeedsWater
function and logging the result is certainly useful, not sure why you call the first without logging the result. Its not needed
Thank you. I might have misunderstood something.
It is not a must to call the function before logging it, you can as well call the function as you are logging. e.g
console.log(plantNeedsWater('Tuesday'));
Thanks this really helped me clear it up, I didn’t understand either why should I call separately before logging.
Glad I could help.
The step number tells us to call the function:
plantNeedsWater(‘Tuesday’);
but actually I tried running it without that and it still runs.
I don’t get why it asks us to do that, when while we’re logging the function we’re already specifying the value as ‘Tuesday’.
in this bit of text:
there are so many that and its (reference words), that the sentence no longer makes much sense
You tried running with 'Tuesday'
argument when calling the function?
Where are you logging?
You didn’t misunderstood the exercise. I did the same, it is just the exercise is asking for that in step 4 and then in step 5 again:
4 Call the plantNeedsWater()
and pass in 'Tuesday'
as an argument.
5 Let’s check that plantNeedsWater()
returned the expected value.
Log plantNeedsWater('Tuesday')
to the console. If it worked correctly, you should see false
logged to the console.
I think the calling of the function expression in this exercise before logging it was just for demonstration purposes. You could also go straight from the function expression to logging it by commenting out the calling before the logging like below, and the code will still work.
const plantNeedsWater = function(day) {
if (day === ‘Wednesday’) {
return true;
} else {
return false;
}
};
//plantNeedsWater(‘Tuesday’);
console.log(plantNeedsWater(‘Tuesday’));