skip to Main Content

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


  1. Here’s an example of what I mean:

    const conversion = (abbreviation, abbreviation1, number) => {
        var combo = abbreviation + abbreviation1;
        console.log(number + ' '+abbreviation + ' to ' + abbreviation1);
        if( combo === 'cf' )
            return number * 9 / 5 + 32;
        if( combo === 'ck' )
            return number + 273.15;
        if( combo === 'fc' )
            return (number - 32) * 5 / 9;
        if( combo === 'fk' )
            return (number - 32) * 5 / 9 + 273.15;
        if( combo === 'kc' )
            return number - 273.15;
        if( combo === 'kf' )
            return (number - 273.15) * 9 / 5 + 32;
        return number;
      };
    };
    console.log( conversion("f","c",100) );
    

    For debug purposes, you could modify this just a bit to use number = instead of return; then you could console.log(number); just before the final return statement.

    Login or Signup to reply.
  2. You could do something like this:

    const conversion = (abbreviation, abbreviation1, number) => {
        switch (abbreviation + abbreviation1) {
            case "cf":
                return (number * 9) / 5 + 32;
            case "ck":
                return number + 273.15;
            case "fc":
                return ((number - 32) * 5) / 9;
            case "fk":
                return ((number - 32) / 1.8) + 273.15;
            case "kc":
                return number - 273.15;
            case "kf":
                return ((number - 273.15) * 9 / 5) + 32;
            default:
                return "Invalid input";
        }
    };
    
    const printConversion = (abbreviation, abbreviation1, number) => {
        const result = conversion(abbreviation, abbreviation1, number);
        console.log(`${number} ${abbreviation} to ${abbreviation1} is ${result}`);
    };
    
    printConversion("f", "c", 100);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search