Unit-converter failing tests on fractions and return

Tell us what’s happening:
Describe your issue in detail here.

The two tests on fractions and return string fail, but I don’t see why. The response in both cases seems to adhere to the requirements.

Your project link(s)

solution: https://unitconvert.herokuapp.com
githubLink: GitHub - danmikes/unitconvert

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.115 Safari/537.36

Challenge: Metric-Imperial Converter

Link to the challenge:

I think the problem is in two places:

(1) a function splits the input in number and string and then an if … else if block checks if number and unit are valid. For some reason all is picked up correctly, but not the case of only a number, without unit. It should return “invalid unit”. But it doesn’t.

function numberStringSplitter(input) {
  let inputString = input;
  let number = inputString.match(/[.\d\/]+/g) || ["1"];
  let string = inputString.match(/[a-zA-Z]+/g)[0];

  return [number[0], string];
}
      if (!initNum && !initUnit) {
        res.send("invalid number and unit");
      } else if (!initNum) {
        res.send("invalid number");
      } else if (!initUnit) {
        res.send("invalid unit");
      }

(2) In the example project the returnNum is given as number, in my case it is given as string. I cannot find where the problem is. It only does it for returnNum

I found why returnNum was a string:

console.log(typeof result);  -> number
console.log(typeof Number(result));  -> number
console.log(typeof Number(result.toFixed(5)));   -> number
console.log(typeof Number(result).toFixed(5));   -> string
console.log(typeof result.toFixed(5));   -> string

Apparently after using toFixed() one must cast to Number

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