Convert Celsius to Fahrenheit - JavaScript Solution & Walkthrough
(01/16) Learn how to solve coding challenges using FreeCodeCamp's curriculum.
01/16 Convert Celsius to Fahrenheit
The algorithm to convert from Celsius to Fahrenheit is the temperature in Celsius times
9/5
, plus32
.You are given a variable
celsius
representing a temperature in Celsius. Use the variablefahrenheit
already defined and assign it the Fahrenheit temperature equivalent to the given Celsius temperature. Use the algorithm mentioned above to help convert the Celsius temperature to Fahrenheit.
function convertToF(celsius) {
let fahrenheit;
return fahrenheit;
}
convertToF(30);
Credit: FreeCodeCamp.org
Understanding the Challenge
The first challenge is this series seems to be a straightforward one.
We are given a given a temperature in celsius
. And what the challenge
requires of us is to write a function convertToF()
that converts the
temperature from celsius
to its equivalent in fahrenheit
.
Fortunately, we have been given the formula.
We are told "The algorithm to convert from Celsius to Fahrenheit is the temperature in Celsius times 9/5
, plus 32
."
So, x celsius is equal to
(x * (9/5)) + 32
fahrenheit.
Pseudocode
Given celsius,
we multiply celsius by (9/5)
we then add the result to 32
return the sum we get after adding 32.
Solving the Challenge
function convertToF(celsius) {
let fahrenheit;
fahrenheit = (celsius * (9/5)) + 32
return fahrenheit;
}
convertToF(30); // 86
Congratulations!
You just cracked the first challenge in this series.
Cheers and happy coding!