JS functions for newbie
Hello!
How does the
getFahrenheit
function know that the return value of multiplyByNineFifths
is the celsius argument?
Taken from codecademy intro course.
11 Replies
In this example it makes sense since all arguments are the same after calling
costOfMonitors
with arguments 5 and 4.
The getFarenheit() function is passing the value (15 in your example) to the multiplyByNineFiths() function.
This multiplyByNineFiths() function is taking the value passed and multiplying it by 1.8 (which for some reason they have decided to define as a devision rather than a hardcoded number, presumably to demonstrate something in the course).
The resulting value (27 in this example) is then returned to the getFarenheit() function where 32 is then added to give the final value.
15 * 1.8 + 32 = 59
I do get the route that this code takes, I just don't understand how 27 becomes
celsius
? Another way of asking is why write celsius? I understand that it helps in terms of understanding the whole code that this value is celsius, but if we changed the word celsius to godzilla, it would still work.
15 is obviously number
, but why is 27 celsius?At no point is 27 Celsius.
In this code, using your example, the value of Celsius is 15, i.e. the initial value that you pass to the getFarenheit() function.
27 is just one part of the final calculation that is needed to convert Celsius to Fahrenheit.
The function is multiplying the number that you passed by 9/5 (or 1.8).
Then the initial function adds 32 to this number which then gives you the final value.
This is just how the calculation from Celsius to Fahrenheit is done.
To convert temperatures in degrees Celsius to Fahrenheit, multiply by 1.8 (or 9/5) and add 32.I'm sorry if I am miss-understanding the question
Oh my god! No, you really didn't misunderstand, you just made it so so obvious for me.
Because getFahrenheit looks back to multiplyByNineFifths, I assumed that 15 was not used in getFahrenheit, but in multiplyByNineFifths.
But of course 15 is celsius, it says so!!!!! Stupido! š¤£
So a way of thinking about this is that
So a way of thinking about this is that
number
is holding celsius
(15), right??!exacto!
getFahrenheit() is calling the parameter "celsius" and passes this multiplyByNineFiths() where it is now called "number" but the value passed (15) remains the same.
getFahrenheit() is calling the parameter "celsius" and passes this multiplyByNineFiths() where it is now called "number" but the value passed (15) remains the same.
And to be extemely pedantic ...
Is the argument
number
holding 15, or is it holding getFahrenheit(15)
?it is holding 15
Fantastic!
you are just passing the value from one function to another
That was really a wall knocked down in terms of reading code!
Thank you so much! You get todays peacock!
š¦