So my code passes both the unit and functional tests but when i run it for the project tests themselves the following test fail:
- You can convert
'gal'
to'L'
and vice versa. (1 gal to 3.78541 L) - You can convert
'lbs'
to'kg'
and vice versa. (1 lbs to 0.453592 kg) - You can convert
'mi'
to'km'
and vice versa. (1 mi to 1.60934 km)
I found the fix is to fix it to 5 decimal points but no matter what I do it doesn’t work for the response, only for the string part. Here’s the related snippet of my code:
convertHandler.js:
// Function to generate the output string
this.getString = function(initNum, initUnit, returnNum, returnUnit) {
const spellOutInitUnit = this.spellOutUnit(initUnit);
const spellOutReturnUnit = this.spellOutUnit(returnUnit);
let result = `${initNum} ${spellOutInitUnit} converts to ${parseFloat(returnNum.toFixed(5))} ${spellOutReturnUnit}`;
return result;
};
api.js:
// Define the route for conversion
app.get('/api/convert', (req, res) => {
let input = req.query.input;
// Extract the number and unit from the input
let initNum = convertHandler.getNum(input);
let initUnit = convertHandler.getUnit(input);
// Validate the unit
const validUnits = ['gal', 'l', 'lbs', 'kg', 'mi', 'km'];
if (!validUnits.includes(initUnit)) {
return res.json({ error: 'invalid unit' });
}
// Validate the number
if (isNaN(initNum)) {
return res.json({ error: 'invalid number' });
}
// Get the return unit and convert the number
let returnUnit = convertHandler.getReturnUnit(initUnit);
let returnNum = convertHandler.convert(initNum, initUnit);
// Generate the output string
let string = convertHandler.getString(initNum, initUnit, returnNum, returnUnit);
// Send the response
res.json({
initNum: initNum,
initUnit: initUnit,
returnNum: parseFloat(returnNum.toFixed(5)),
returnUnit: returnUnit,
string: string
});
});
basically when I input “1lbs” the string is “1 pounds converts to 0.45359 kilograms” which is great but then the response under it is {“initNum”:1,“initUnit”:“lbs”,“returnNum”:0.453592,“returnUnit”:“kg”,“string”:“1 pounds converts to 0.45359 kilograms”} where returnNum is the full decimal value.