Mocha test for functionality of Convert.kmToM()

Hello everyone,

I am working on the Exam Part 2 from Front-end development path (https://www.codecademy.com/exams/journeys/full-stack-engineer/paths/fscj-22-front-end-development/parts/2) and I am stuck at this exercise:

" Run the mocha test.js command in the terminal to test the functionality of Convert.kmToM(). The two tests should fail!
Interpret the error messages and make changes to the .kmToM() method to pass both tests. Run the mocha test in test.js to see both tests passing."

This is what I did:

const Convert = {
  kmToM(inputValue){
    return Number(((inputValue * 0.621371192).toFixed(2)));
  }
}

module.exports = Convert;

but when I run mocha test.js I get this error

I can’t figure why the second decimal is different. Could anyone please help me?

Thank you!

Okay. Have you tried type “return Number(((inputValue / 1.609).toFixed(2)));”. I did type it in. It worked. You just need to match the number. I hope that helps.

It might be due to how JavaScript handles floating-point precision. When you use the toFixed(2) method on a number, it rounds the number to two decimal places. However, this rounding may not be exact, especially for numbers that have many digits after the decimal point.

Try using the Math.round function instead of toFixed. The Math.round function rounds a number to the nearest integer. You can then multiply the result by 100 (to move the decimal point two places to the right) and divide by 100 (to move the decimal point two places to the left).

const Convert = {
  kmToM(inputValue){
    return Number((Math.round(inputValue * 0.621371192 * 100) / 100).toFixed(2));
  }
}

Try this out, and let us know.