Hi folks,
I’m having some trouble identifying why I keep failing this specific user story for my Metric-Imperial Converter for the QA-certificate project:
I can convert
'mi'
to'km'
and vice versa. (1 mi to 1.60934 km)
I pass all other user stories (including all unit and functional tests), and I have compared the output with the example app as well to no avail. Here are some snippets of my relevant code:
convertHandler.js
function ConvertHandler() {
const convertionObject = {
gal: {
factor: 3.78541,
name: "gallons",
newUnit: "L"
},
L: {
factor: 0.264172,
name: "liters",
newUnit: "gal"
},
lbs : {
factor: 0.453592,
name: "pounds",
newUnit: "kg"
},
kg: {
factor: 2.204624,
name: "kilograms",
newUnit: "lbs"
},
mi: {
factor: 1.60934,
name: "miles",
newUnit: "km"
},
km: {
factor: 0.621371,
name: "kilometers",
newUnit: "mi"
}
};
const roundTo5 = num => Math.round(num * 100000) / 100000;
const parseFraction = (fraction) => {
const [numerator, denominator] = fraction.split("/");
if (isNaN(numerator) || isNaN(denominator)) {
return "";
}
if (!numerator || !denominator) {
return "";
}
if (fraction.split("/").length !== 2) {
return "";
}
return numerator / denominator;
};
this.getNum = function(input) {
let result;
try {
result = input.match(/[\d|\.|\/]+/)[0];
} catch(err) {
result = "1";
}
if (result.includes("/")) {
result = parseFraction(result);
}
if (isNaN(result)) {
result = "";
}
return result;
};
this.getUnit = function(input) {
let result;
try {
result = input.match(/[a-zA-Z]+/)[0].toLowerCase();
} catch(err) {
result = "";
}
if (result === "l") { result = "L"; }
if (!convertionObject.hasOwnProperty(result)) {
result = "";
}
// console.log(`Unit is ${result}.`);
return result;
};
this.getReturnUnit = function(initUnit) {
let result = convertionObject[initUnit].newUnit;
return result;
};
this.spellOutUnit = function(unit) {
let result = convertionObject[unit].name;
return result;
};
this.convert = function(initNum, initUnit) {
let result = initNum * convertionObject[initUnit].factor;
result = roundTo5(result);
return result;
};
this.getString = function(initNum, initUnit, returnNum, returnUnit) {
let result = `${initNum} ${this.spellOutUnit(initUnit)} converts to ${returnNum} ${this.spellOutUnit(returnUnit)}`;
return result;
};
}
module.exports = ConvertHandler;
api.js
'use strict';
const expect = require('chai').expect;
const ConvertHandler = require('../controllers/convertHandler.js');
module.exports = function (app) {
let convertHandler = new ConvertHandler();
app.route('/api/convert')
.get(function (req, res){
let input = req.query.input;
let initNum = convertHandler.getNum(input);
let initUnit = convertHandler.getUnit(input);
if (!initNum && !initUnit) {
return res.send("invalid number and unit");
} else if (!initNum) {
return res.send("invalid number");
} else if (!initUnit) {
return res.send("invalid unit");
}
let returnNum = convertHandler.convert(initNum, initUnit);
let returnUnit = convertHandler.getReturnUnit(initUnit);
let toString = convertHandler.getString(initNum, initUnit, returnNum, returnUnit);
res.json({
initNum: parseFloat(initNum),
initUnit: initUnit,
returnNum: returnNum,
returnUnit: returnUnit,
string: toString
});
});
};
The full repl.it repo is linked below:
https://repl.it/@thewchan/boilerplate-project-metricimpconverter
The confusing thing is that I’ve pretty much implemented all conversion the same way, yet the km ↔ mi one is the only one failing?
Thanks in advance to any help anyone to give!