Metric-Imperial Converter getNum() weird error

Hi, I’m actually trying to complete the first project of Information Security and Quality Assurance Projects. The problem is magically this function return doesn’t past the test. I tried with quincys example and it worked. Now the f@#~@g magic is that if you try it with the console or the api itself it works it returns 32. I try setting the return to a string a it did not work.

this.getNum = function(input) {
    let result;
    // Find index of unit
    let index = /[a-z]/.exec(input).index;
    // If index === 0 there are no number. Default 1
    if (index === 0) {
      return 1;
    // Let split the input copy it from 0 to Index
    let num = input.slice(0, index);

    if (input.indexOf("/") == -1) {
      result = Number(num);
    } else {
      inputArr = num.split("/");
      if (inputArr > 2) {
        return "invalid number";
      result = Number(inputArr[0]) / Number(inputArr[1]);
    return result;

This is the test

test("Whole number input", function(done) {
      var input = "32L";
        "Int es returned correctly"

Hopefully you got it solved by now without help, but here’s what I see:

It might be the regex you’re splitting the string by. You’re only looking for lowercase, but you don’t have the ‘i’ flag up.

let index = /[a-z]/.exec(input).index;

This won’t find uppercase L but will find lowercase, so you’re not removing uppercase L when you slice off the unit a couple lines further.

let index = /[a-z]/i.exec(input).index;

This will find ‘l’ or ‘L’.

It’s interesting that it’s working outside of the tests. If you use ‘l’ instead of ‘L’ in your manual tests that might explain the difference.
Also, do you do anything to process the input in /routes/api.js? If you’re setting the input .toLower() in the route handler it won’t work for the unit tests because they bypass routes/api.js and call convertHandler directly.

1 Like

Hi, indeed it was the problem. Exactly as you describe it. I already got all certificates except D3 part, so yes I complete it.
Thanks for answering