Why do I need to call the function vs logging it?

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?

2 Likes

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.

2 Likes

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?

1 Like

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.