I can't Passed A Test Metric/Imperial Converter

I can’t pass this test

  1. All incoming units should be accepted in both upper and lower case, but should be returned in both the initUnit and returnUnit in lower case, except for liter, which should be represented as an uppercase 'L' . (It seems work to me)
  2. Your return will consist of the initNum , initUnit , returnNum , returnUnit , and string spelling out units in the format '{initNum} {initUnitString} converts to {returnNum} {returnUnitString}' with the result rounded to 5 decimals.(Its seems work to me to)
    What Can I do, Here is the repo and repl:
    Sereysopea-Ung/Metric-Imperial-Converter: To learn Freecodecamp (github.com)
    Metric/Imperial Converter (sereysopea-ung.repl.co)

Here is my code:

function ConvertHandler() {
  
  this.getNum = function(input) {
    if (['kg','lbs','gal','l','mi','km'].indexOf(input.toLowerCase()) !== -1){
      return 1;
    }
    return parseFloat(input);
  };
  
  this.getUnit = function(input) {
    if (['kg','lbs','gal','l','mi','km'].indexOf(input.toLowerCase()) !== -1){
      if (input=='L' || input=='l'){
        return input.toUpperCase();
      }
      return input.toLowerCase();
    }
    return input.substr((parseFloat(input)+'').length)=='l' ? 'L' : (input.substr((parseFloat(input)+'').length)).toLowerCase();
  };
  
  this.getReturnUnit = function(initUnit) {
    switch (initUnit.toLowerCase()){
      case 'kg':
        return 'lbs';
      case 'lbs':
        return 'kg';
      case 'gal':
        return 'L';
      case 'l':
        return 'gal';
      case 'km':
        return 'mi';
      case 'mi':
        return 'km';
      default:
        return false;
    }
  };

  this.spellOutUnit = function(unit) {
    switch(unit){
      case 'kg':
        return 'kilograms';
      case 'gal':
        return 'gallons';
      case 'lbs':
        return 'pounds';
      case 'L':
        return 'liters';
      case 'km':
        return 'kilometers';
      case 'mi':
        return 'miles';
    }
  };
  
  this.convert = function(initNum, initUnit) {
    const galToL = 3.78541;
    const lbsToKg = 0.453592;
    const miToKm = 1.60934;
    switch (initUnit){
      case 'kg':
        return (initNum / lbsToKg).toFixed(5);
      case 'gal':
        return (initNum * galToL).toFixed(5);
      case 'lbs':
        return (initNum * lbsToKg).toFixed(5);
      case 'L':
        return (initNum / galToL).toFixed(5);
      case 'mi':
        return (initNum * miToKm).toFixed(5);
      case 'km':
        return (initNum / miToKm).toFixed(5);
    }
  };
  
  this.getString = function(initNum, initUnit, returnNum, returnUnit) {
    if (this.getReturnUnit(initUnit)){
      return {
        initNum: initNum,
        initUnit: initUnit,
        returnNum: returnNum,
        returnUnit: returnUnit,
        string: initNum+' '+this.spellOutUnit(initUnit)+' converts to '+returnNum+' '+this.spellOutUnit(returnUnit)
      };
    }
    return 'invalid Unit';
  };
  
}

module.exports = ConvertHandler;


This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.