Metric-Imperial Converter: Cannot identify why I'm failing "km" <-> "mi" conversion user story

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!

Have to tried changing the unit? Everything else works so if it is failing that conversion logical conclusion… there’s an incorrect unit perhaps?

factor: 0.6214 instead?

1 Like

So the test asks you to convert ‘mi’ to ‘km’ , and vice versa. You however are not doing that in your code.

If you take a closer look at what you are converting ‘mi’ to and from, you will realize the issue here.

hint:

Check the results of these conversions:

1 lbs = 0.45359 kg?
10 lbs = 4.53592 kg?
1 kg = 2.20462 lbs?
10 kg = 22.04624?

Thanks for responding! I think you’re right, there must be a tighter tolerance for the conversion than I anticipated. I should have just used more sig figs for my conversion and round afterwards. That’s really what solved it. Thanks!

1 Like

Not sure what you mean by me not doing the conversion in my code, because… That’s totally what I’m doing. All the other user stories as related to lbs/kg and gal/L works.

I was warned by moderators before for giving the solution so I tried to be more discrete when trying to help you :sweat_smile:

If you get stuck for a long time you can always check freeCodeCamp github repo to see what the tests are exactly asking for.

Cool yeah as I mentioned above looks like it’s just a sig fig error, I needed to add more digits before I do the rounding. Thanks tho