-1

I am trying to write some logic to convert Celsius to Fahrenheit and when passed a negative value, it returns NaN. Not sure why. Here is my Fahrenheit offset:

const fahrenheitOffset = (value) => (value * 1.8) + 32;

and here is my function:

function celsiusToFarenheit(celsiusValue) {
  return parseFloat((fahrenheitOffset(celsiusValue)).toFixed(2));
}

and another function to convert it to a string:

function celsiusToFarenheitString(value) {
  return `${parseFloat(fahrenheitOffset(value)).toFixed(1)}°F`;
}

when I run these tests, I get NaN rather than '-0.55'

    test('returns Farenheit float if Farenheit output is requested and valid reading passed', () => {
      expect(temperatureConverter(
        '-0.55',
        unitTypesEnums.FAHRENHEIT.string,
        converterReturnTypeEnums.FLOAT)
      ).toEqual(celsiusToFarenheit(parsed));
    });

    test('returns Fahrenheit string if Fahrenheit output is requested and valid reading passed in', () => {
      expect(temperatureConverter(
        '-0.55',
        unitTypesEnums.FAHRENHEIT.string,
        converterReturnTypeEnums.STRING)
      ).toEqual(celsiusToFarenheitString(parsed));
    });
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
StuffedPoblano
  • 675
  • 1
  • 12
  • 35

1 Answers1

0

I think the problems are in the test not parseFloat. This snippet logs -67 in each format.

const fahrenheitOffset = (value) => (value * 1.8) + 32;

function celsiusToFarenheit(celsiusValue) {
  console.log(parseFloat((fahrenheitOffset(celsiusValue)).toFixed(2)));
}
function celsiusToFarenheitString(value) {
console.log(`${parseFloat(fahrenheitOffset(value)).toFixed(1)}°F`);
}

celsiusToFarenheit(-0.55)
celsiusToFarenheitString(-0.55)
stever
  • 1,232
  • 3
  • 13
  • 21
  • The -0.55 value is the fahrenheitOffset or the celsius value. – StuffedPoblano Jan 25 '19 at 19:54
  • Converts -55C to -67F. https://www.google.com/search?q=convert+-55+celcius+to+farenheight&oq=convert+-55+celcius+to+farenheight&aqs=chrome..69i57.11714j0j7&sourceid=chrome&ie=UTF-8 – stever Jan 25 '19 at 19:57
  • I changed inputs to -0.55 to match your test. returns 31.01 31.0°F – stever Jan 25 '19 at 20:04