Reputation: 1
Doing a Celsius to Fahrenheit converter and reverse. and code seems to be working fine but when I give 20 under the F to C function it returns 2.2222 instead of 68 like it should, does the same with both equations.
let num = userInput
let numF = num * 9 / 5 + 32;
let numC = num - 32 * 5 / 9;
celci = (num) => {
console.log('Enter temp to convert')
if (num <= 0 || num >= 0) {
return numF
}
}
if (num === 'F') {
celci(num)
}
If I enter F it returns 'Enter temp to convert' like it should, but when I give it a number it returns the completely wrong answer.
Upvotes: 0
Views: 54
Reputation: 31
There is a mistake in the precedence of the operators. It is better to use brackets in complex equations. You should use:
let numC = (num - 32)* (5/ 9);
Upvotes: 0
Reputation: 147413
Be careful of operator precedence. *
and \
are performed before +
and -
(which is the same as for mathematic equations). So in:
num - 32 * 5 / 9
your equation is effectively:
num - (32 * 5 / 9)
but should be:
(num - 32) * 5 / 9;
The first equation should work as the +
is at the end anyway.
Upvotes: 1