2

My function is to either input a Fahrenheit and output the conversion to Celsius or input Celsius and output the conversion to Fahrenheit. My test challenges that I round whatever result to the first decimal, which I've done correctly. However my test says otherwise, I'm using 'Jasmine' to test the code. Here is what I've got.

const ftoc = function(fahr) {
  let input = fahr;
  if (typeof fahr === 'number'){
  let result = (fahr - 32) * 5/9;
  if (Number.isInteger(result) === false) {
    return result.toFixed(1);
  } else {return result}
  }
}

const ctof = function(celc) {
  let input = celc;
  if (typeof input === 'number') {
    let result = celc * (9/5) + 32;
    if (Number.isInteger(result) === false) {
    return result.toFixed(1);
    } else {return result}
  }
}

module.exports = {
  ftoc,
  ctof
}

and here are the tests

const {ftoc, ctof} = require('./tempConversion')

describe('ftoc', function() {
  it('works', function() {
    expect(ftoc(32)).toEqual(0);
  });
  it('rounds to 1 decimal', function() {
    expect(ftoc(100)).toEqual(37.8);
  });
  it('works with negatives', function() {
    expect(ftoc(-100)).toEqual(-73.3);
  });
});

describe('ctof', function() {
  it('works', function() {
    expect(ctof(0)).toEqual(32);
  });
  it('rounds to 1 decimal', function() {
    expect(ctof(73.2)).toEqual(163.8);
  });
  it('works with negatives', function() {
    expect(ctof(-10)).toEqual(14);
  });
});

My errors would be as follows: Expected '163.8' to equal 163.8. Expected '37.8' to equal 37.8. Expected '-73.3' to equal 73.3.

Seems to be expecting some sort of extra period after the numerical result and I'm not sure why that is. Thanks!

James Ross
  • 67
  • 1
  • 7

1 Answers1

1

Your functions are returning a string, so just update your expect to:

expect(ftoc(100)).toEqual("37.8");

And it will work.

The reason is because .toFixed returns a string by default, as documented here.

Bruno Monteiro
  • 4,153
  • 5
  • 29
  • 48