Metric-Imperial Converter - App works, tests fail

Tell us what’s happening:

Once again, the app works, and all unit and functional tests pass, but the following tests fail when I submit my link:

I can convert 'gal' to 'L' and vice versa. (1 gal to 3.78541 L)
I can convert 'lbs' to 'kg' and vice versa. (1 lbs to 0.453592 kg)
I can convert 'mi' to 'km' and vice versa. (1 mi to 1.60934 km)
If my unit of measurement is invalid, returned will be 'invalid unit' .

So I compared my app’s responses to those of the example project:

“I can convert ‘gal’ to ‘L’ and vice versa. (1 gal to 3.78541 L)”

example: {“initNum”:1,“initUnit”:“gal”,“returnNum”:3.78541,“returnUnit”:“l”,“string”:“1 gallons converts to 3.78541 liters”}
mine: {“initNum”:1,“initUnit”:“gal”,“returnNum”:3.78541,“returnUnit”:“l”,“string”:“1 gallons converts to 3.78541 liters”}

example: {“initNum”:3.78541,“initUnit”:“l”,“returnNum”:1,“returnUnit”:“gal”,“string”:“3.78541 liters converts to 1 gallons”}
mine: {“initNum”:3.78541,“initUnit”:“l”,“returnNum”:1,“returnUnit”:“gal”,“string”:“3.78541 liters converts to 1 gallons”}

I can convert ‘lbs’ to ‘kg’ and vice versa. (1 lbs to 0.453592 kg)

example: {“initNum”:1,“initUnit”:“lbs”,“returnNum”:0.45359,“returnUnit”:“kg”,“string”:“1 pounds converts to 0.45359 kilograms”}
mine: {“initNum”:1,“initUnit”:“lbs”,“returnNum”:0.453592,“returnUnit”:“kg”,“string”:“1 pounds converts to 0.453592 kilograms”}

{“initNum”:0.45359,“initUnit”:“kg”,“returnNum”:1,“returnUnit”:“lbs”,“string”:“0.45359 kilograms converts to 1 pounds”}
{“initNum”:0.453592,“initUnit”:“kg”,“returnNum”:1,“returnUnit”:“lbs”,“string”:“0.453592 kilograms converts to 1 pounds”}

“I can convert ‘mi’ to ‘km’ and vice versa. (1 mi to 1.60934 km)”

{“initNum”:1,“initUnit”:“mi”,“returnNum”:1.60934,“returnUnit”:“km”,“string”:“1 miles converts to 1.60934 kilometers”}
{“initNum”:1,“initUnit”:“mi”,“returnNum”:1.60934,“returnUnit”:“km”,“string”:“1 miles converts to 1.60934 kilometers”}

{“initNum”:1.60934,“initUnit”:“km”,“returnNum”:1,“returnUnit”:“mi”,“string”:“1.60934 kilometers converts to 1 miles”}
{“initNum”:1.60934,“initUnit”:“km”,“returnNum”:1,“returnUnit”:“mi”,“string”:“1.60934 kilometers converts to 1 miles”}

“If my unit of measurement is invalid, returned will be ‘invalid unit’.”

{“error”:“invalid unit”} // example
{“error”:“invalid unit”} // mine

The only difference is that the example will truncate or round the input to 5 decimal places if it is longer. I also compared response status codes, and mine are identical to the example: 200, or 400 for bad input.

Interestingly, the tests: “If my number is invalid, returned with will ‘invalid number’.” and
“If both are invalid, return will be ‘invalid number and unit’.” both pass for me. I checked to make sure there isn’t a spelling error in {“error”:“invalid unit”}. There isn’t. Also when I paste in the link for the example app, it also fails this test! Yet it somehow passes the conversion tests, even though in most cases my app’s response bodies are character-for-character identical, and when they’re not, it’s because of a difference of like 0.000002.

Once again, either there is a problem with the tests, or the user stories aren’t clearly explaining the actual requirements. It seems like I need to be able to debug the FCC test code to figure out what the problem is. It’s getting a bit frustrating!

Your code so far

Repl.it Link

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36.

Challenge: Metric-Imperial Converter

Link to the challenge:

Hello!

The tests are checking for only 5 decimal numbers, so limit the result to 5 decimal places and you should pass them.

And yes, the description doesn’t tell you about the 5 decimal places.

1 Like

I added a console.log() of your res.json() to both my project and yours and these are the results of running the submission tests (it’s long).

As @skaparate indicated, the reported places are not correct. Mine are all strings as well, and I can’t remember if that was an issue with the tests or not. The number of places to keep is stated in the user stories (views/index.html).

Mine:

{
  initNum: 1,
  initUnit: 'gal',
  returnNum: '3.78541',
  returnUnit: 'l',
  string: '1 gallons converts to 3.78541 liters'
}
{
  initNum: 10,
  initUnit: 'gal',
  returnNum: '37.85410',
  returnUnit: 'l',
  string: '10 gallons converts to 37.85410 liters'
}
{
  initNum: 1,
  initUnit: 'l',
  returnNum: '0.26417',
  returnUnit: 'gal',
  string: '1 liters converts to 0.26417 gallons'
}
{
  initNum: 10,
  initUnit: 'l',
  returnNum: '2.64172',
  returnUnit: 'gal',
  string: '10 liters converts to 2.64172 gallons'
}
{
  initNum: 1,
  initUnit: 'lbs',
  returnNum: '0.45359',
  returnUnit: 'kg',
  string: '1 pounds converts to 0.45359 kilograms'
}
{
  initNum: 10,
  initUnit: 'lbs',
  returnNum: '4.53592',
  returnUnit: 'kg',
  string: '10 pounds converts to 4.53592 kilograms'
}
{
  initNum: 1,
  initUnit: 'kg',
  returnNum: '2.20462',
  returnUnit: 'lbs',
  string: '1 kilograms converts to 2.20462 pounds'
}
{
  initNum: 10,
  initUnit: 'kg',
  returnNum: '22.04624',
  returnUnit: 'lbs',
  string: '10 kilograms converts to 22.04624 pounds'
}
{
  initNum: 1,
  initUnit: 'mi',
  returnNum: '1.60934',
  returnUnit: 'km',
  string: '1 miles converts to 1.60934 kilometers'
}
{
  initNum: 10,
  initUnit: 'mi',
  returnNum: '16.09340',
  returnUnit: 'km',
  string: '10 miles converts to 16.09340 kilometers'
}
{
  initNum: 1,
  initUnit: 'km',
  returnNum: '0.62137',
  returnUnit: 'mi',
  string: '1 kilometers converts to 0.62137 miles'
}
{
  initNum: 10,
  initUnit: 'km',
  returnNum: '6.21373',
  returnUnit: 'mi',
  string: '10 kilometers converts to 6.21373 miles'
}

Yours:

{
  initNum: 1,
  initUnit: 'gal',
  returnNum: 3.78541,
  returnUnit: 'l',
  string: '1 gallons converts to 3.78541 liters'
}
{
  initNum: 10,
  initUnit: 'gal',
  returnNum: 37.8541,
  returnUnit: 'l',
  string: '10 gallons converts to 37.8541 liters'
}
{
  initNum: 1,
  initUnit: 'l',
  returnNum: 0.26417217685798894,
  returnUnit: 'gal',
  string: '1 liters converts to 0.26417217685798894 gallons'
}
{
  initNum: 1,
  initUnit: 'lbs',
  returnNum: 0.453592,
  returnUnit: 'kg',
  string: '1 pounds converts to 0.453592 kilograms'
}
{
  initNum: 1,
  initUnit: 'mi',
  returnNum: 1.60934,
  returnUnit: 'km',
  string: '1 miles converts to 1.60934 kilometers'
}
{
  initNum: 10,
  initUnit: 'mi',
  returnNum: 16.0934,
  returnUnit: 'km',
  string: '10 miles converts to 16.0934 kilometers'
}
{
  initNum: 1,
  initUnit: 'km',
  returnNum: 0.6213727366498067,
  returnUnit: 'mi',
  string: '1 kilometers converts to 0.6213727366498067 miles'
}
2 Likes

Thanks!

I truncated both the input number before conversion and the output number after conversion to 5 places, and now those test pass.

But “If my unit of measurement is invalid, returned will be 'invalid unit' .” still fails. The example app also fails on this test.

And I figured out why: The response status code has to be 200. Only for this one. :face_with_raised_eyebrow: The other two invalid input responses pass with 400 code. Also, it makes no difference whether you do res.send(“error message”) or res.json(“error”: “error message”).

2 Likes

I found out that it only accepts res.send('invalid unit'); now.

1 Like