Im working on making my code more concise and eliminate junk code. I made a temperature calculator that converts temperatures between Fahrenheit Celsius and kelvin. What changes to the following code would you recommend i make it more concise? and im having a hard time printing the result of the function to the console.log within the function, i would like to print something like this "10 f to c is 37.7778" i dont know how to print the result of the function into the console.log, my spider sense is telling it may have something to do with loops but im fairly new and im lost. Thank you
const conversion = (abbreviation, abbreviation1, number) => {
if (abbreviation === "c"&&abbreviation1==='f') {console.log((number * 9) / 5 + 32);}
else if (abbreviation==='c' && abbreviation1==='k'){console.log(number+273.15)}
else if (abbreviation === "f" && abbreviation1==='c'){console.log(((number - 32) * 5) / 9);}
else if (abbreviation === "f" && abbreviation1==='k'){console.log((number-32)/1.8+273.15)}
else if (abbreviation==='k'&& abbreviation1==='c'){console.log(number-273.15)}
else if (abbreviation==='k' && abbreviation1==='f')
{console.log(((number - 273.15) * 9/5 +32));
};
console.log(number + ' '+abbreviation + ' to ' + abbreviation1+' is' + **conversion(abbreviation, abbreviation1, number)**
};
conversion("f","c", 100);
I tried using loops but i still cant get the result i want.
2
Answers
Here’s an example of what I mean:
For debug purposes, you could modify this just a bit to use
number =
instead ofreturn
; then you couldconsole.log(number);
just before the final return statement.You could do something like this: