Why tweak to getFahrenheit works the way it does


#1

Hi there,

I was looking at the getFahrenheit function in the instructions and wondered if I could tweak what was there so that I didn’t have to write the console.log command, like they do, to get it to log the temperature; I wanted to be able to just call a function that would write the sentence for me. So I added a function called summary, as below:

const multiplyByNineFifths = (celsius) => {
  return celsius * (9/5);
};

const getFahrenheit = (celsius) => {
  return multiplyByNineFifths(celsius) + 32;
};

//my addition
const summary = (celsius) => {
  console.log('The temperature is ' + getFahrenheit(celsius) + ' degrees outside.')
};

Now, calling the summary function with a value for the celsius parameter will log the sentence to the console automatically. For example, calling this:


summary(15);

Logs:

The temperature is 59 degrees outside.

But I’m really trying to figure out is how all these functions and parameters work together. For example, when I tweaked the above functions again to remove the celsius parameter from getFahrenheit inside the summary function, like so (just to see what would happen):

const multiplyByNineFifths = (celsius) => {
  return celsius * (9/5);
};

const getFahrenheit = (celsius) => {
  return multiplyByNineFifths(celsius) + 32;
};

const summary = (celsius) => {
  console.log('The temperature is ' + getFahrenheit/* NO CELSIUS*/ + ' degrees outside.')
}

And called the function again:

summary(15);

This is what was logged:

The temperature is (celsius) => {
** return multiplyByNineFifths(celsius) + 32;**
} degrees outside.

Here are my questions:

  1. When you have a function with a parameter, do you always need to include the parameter any other times you call the function, even if it’s in another function?
  2. What exactly is happening in the second case to make it log the message it is?

Thanks!


#2

well, there are two different things at play here, calling the function without argument:

getFahrenheit()

which will cause the parameter of getFahrenheit to be undefined (in JS, in python this would throw an error that you didn’t satisfy the parameter)

but functions are stored in variables, so getFahrenheit() would be a function call, getFahrenheit is simply the variable holding the function, this will cause the function itself to get logged to the console

this allows us to make copies of functions:

x = console.log
x("hello world") // logs hello world to the console

#3

Ok, I think I understand. Thanks!


#4

i made an edit, i screwed up


#5

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