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.
function multiplyByNineFifths(number) {
return number * (9 / 5);
// Returns 27
};

// Celsius is value of 27
function getFahrenheit(celsius) {
return multiplyByNineFifths(celsius) + 32;
// Returns 59
};

// Calling getFahrenheit, which looks back to multiplyByNineFifths
console.log(getFahrenheit(15));
// Prints 59
function multiplyByNineFifths(number) {
return number * (9 / 5);
// Returns 27
};

// Celsius is value of 27
function getFahrenheit(celsius) {
return multiplyByNineFifths(celsius) + 32;
// Returns 59
};

// Calling getFahrenheit, which looks back to multiplyByNineFifths
console.log(getFahrenheit(15));
// Prints 59
11 Replies
ƅ Marlon G
ƅ Marlon Gā€¢14mo ago
In this example it makes sense since all arguments are the same after calling costOfMonitors with arguments 5 and 4.
function monitorCount(rows, columns) {
return rows * columns;
}

function costOfMonitors(rows, columns) {
return monitorCount(rows, columns) * 200;
}

const totalCost = costOfMonitors(5, 4);
console.log(totalCost)
function monitorCount(rows, columns) {
return rows * columns;
}

function costOfMonitors(rows, columns) {
return monitorCount(rows, columns) * 200;
}

const totalCost = costOfMonitors(5, 4);
console.log(totalCost)
Chris Bolson
Chris Bolsonā€¢14mo ago
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
ƅ Marlon G
ƅ Marlon Gā€¢14mo ago
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?
Chris Bolson
Chris Bolsonā€¢14mo ago
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
ƅ Marlon G
ƅ Marlon Gā€¢14mo ago
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 number is holding celsius(15), right??
Chris Bolson
Chris Bolsonā€¢14mo ago
!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.
ƅ Marlon G
ƅ Marlon Gā€¢14mo ago
And to be extemely pedantic ... Is the argument number holding 15, or is it holding getFahrenheit(15)?
Chris Bolson
Chris Bolsonā€¢14mo ago
it is holding 15
ƅ Marlon G
ƅ Marlon Gā€¢14mo ago
Fantastic!
Chris Bolson
Chris Bolsonā€¢14mo ago
you are just passing the value from one function to another
ƅ Marlon G
ƅ Marlon Gā€¢14mo ago
That was really a wall knocked down in terms of reading code! Thank you so much! You get todays peacock! šŸ¦š
Want results from more Discord servers?
Add your server
More Posts