Logging function result AND its arguments when calling a function

After doing the function and array lessons, I went back to upgrade my Javascript Kelvin Weather project. https://www.codecademy.com/courses/introduction-to-javascript/projects/kelvin-weather-javascript

I’m trying to create a function to perform any selected conversion among four temp scales – Celsius, Fahrenheit, Kelvin and Newtons so that when it’s called, it logs both the final result and the arguments – ie, “100 Celsius equals 212 Fahrenheit.”

So far, everything works, including the error message, except that when it runs, I can only log “The result equals 212.” when I call the function. No arguments.

Function overview, so far.

(1) accepts parameters for (scale1, scale2, temp)
(2) validates the arguments using an array and if statements, and returns a message for invalid entries.
(3) converts the input temp from scale1 to scale2 using nested ifs and some fun math. Discovered rounding to two digits.

When I call the function, I want it to log a message to the console containing both the final result and the arguments – ie, “100 Celsius equals 212 Fahrenheit.”

But all I can do is log the final result. I can’t get it to recognize and log the arguments, from either inside the function or out. I’ve tried the arguments method and returning them as variables, lots of other ideas – everything I could find or think of – but nothing works. I’m sure there’s some helper function or something I should do, but I have no idea how to make that work.

Here is my code:

function tempConverter(scale1, scale2, temp) {

//list of temp scales
const scales = ['Celsius', 'Fahrenheit', 'Kelvin', 'Newtons'];

//test that arguments are valid

if ((scales.indexOf(scale1) === -1) || 
     (scales.indexOf(scale2) === -1) ||
     (typeof temp !== 'number') ||
      (scale1 === scale2)) {
    return 'Sorry, scales must be Celsius, Fahrenheit, Kelvin or Newtons. They cannot be the same, and temp must be a number. Please try again.';
  }
  //calculate conversions

  else if (scale1 === 'Celsius') {
    if (scale2 === 'Kelvin') {
       return temp + 273;
    } else if (scale2 === 'Fahrenheit') {
       return Math.round(((temp * 1.8) + 32) * 100) / 100;
    } else if (scale2 === 'Newtons') {
       return Math.round(temp * .33 * 100) / 100;
       }
    }
  else if (scale1 === 'Fahrenheit') {
    if (scale2 === 'Celsius') {
      return Math.round((temp - 32) / 1.8 * 100) / 100;
    } else if (scale2 === 'Kelvin') {
       return Math.round((((temp - 32) / 1.8) + 273) * 100) / 100;
    } else if (scale2 === 'Newtons') {
       return  Math.round((temp - 32) * 1.833 * 100) / 100;
    }
  }
else if (scale1 === 'Kelvin') {
    if (scale2 === 'Celsius') {
      return temp - 273;
    } else if (scale2 === 'Fahrenheit') {
       return Math.round((((temp - 32) / 1.8) - 273) * 100) / 100;
    } else if (scale2 === 'Newtons') {
       return Math.round((temp - 273) * 33) / 100;
    }
  }
 else if (scale1 === 'Newtons') {
    if (scale2 === 'Celsius') {
      return Math.round((temp / .33) * 100) / 100;
    } else if (scale2 === 'Fahrenheit') {
       return Math.round(((temp * 5.4545) + 32) * 100) / 100;
    } else if (scale2 === 'Kelvin') {
       return Math.round(((temp / .33) + 273 ) * 100) / 100;
    }
  }
}
console.log(`The result is ${tempConverter('Celsius','Fahrenheit',100)}`);
//returns "The result is 212."

// I want to do console.log(`${temp} ${scale1} equals ${finalTemp} ${scale2}`);

Can anyone help me? I looked everywhere on line and couldn’t find out how to include the arguments in the call. Maybe it’s in a later lesson, but it’s driving me nuts.

Thanks!!

Since this is now your code, and no longer needs to satisfy any lesson you could log everything inside the function (but still return a number for further computation).

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.