question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Quality Assurance Projects - Metric-Imperial Converter Example Project accepts invalid input

See original GitHub issue

Describe your problem and how to reproduce it: I am working through the Unit and Functional tests for the Metric Imperial Converter and using the example project to verify my understanding. The example project accepts invalid inputs that are supposed to be rejected according to what is being filled out in the test suite. The specific one being tested is double fraction input. From the 2_functional-test.js file:

      test('Convert 3/7.2/4kg (invalid number)', function(done) {

        //done();
      });

While it is not listed as a unit or functional test the example will also accept division by zero i.e. 3/0L

Add a Link to the page with the problem: https://metric-imperial-converter--freecodecamp.repl.co/

Tell us about your browser and operating system:

  • Browser Name: Mozilla Firefox
  • Browser Version: 80.0
  • Operating System: Windows 10

If possible, add a screenshot here (you can drag and drop, png, jpg, gif, etc. in this box): Screen Shot 2020-08-25 at 5 34 21 PM Screen Shot 2020-08-25 at 5 35 34 PM

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:13 (13 by maintainers)

github_iconTop GitHub Comments

1reaction
SaintPetercommented, Sep 21, 2020

I took a stab at this again. This is very much an “onion” problem - many layers and they all make you cry.

Here are the assumptions:

  1. Units are at lest 1 alphabetic characters preceded by the start of the input or a non-alphabet character and ending at the end of the input
  2. The numbers are anything left in the string once we remove the units with the prior characteristics
  3. A number is made up of numeric digits and zero or one decimals
  4. Division is always a / preceded by and succeeded by a single digit.
// Takes a string in, gives a true/false out
  this.numberChecker= function (num){
    // Check for non-digits and non-periods
    if(num.match(/[^0-9.]/gi)) {
      return false;
    }

    // Check for doubled periods
    let result = num.match(/\./gi);
    if(result && result.length > 1) {
      return false;
    }

    return true;
  }

  let re_units = /(?<=^|[^a-z])([a-z]+)$/ig
  this.getNum = function(input) {
    // get and remove units
    let noUnits = input.replace(re_units,'')

    // No number passed
    if(noUnits.length === 0) {
      return 1;
    }

    // Check for division
    // Valid division is a slash with a number
    // before and after
    let parts = noUnits.split(/(?<=\d)\/(?=\d)/ig);
    if(parts.length === 2) {
      if(!this.numberChecker(parts[0]) || !this.numberChecker(parts[1])) {
        return null;
      }
      // Division Present
      try {
        let numerator = parseFloat(parts[0]);
        let denominator = parseFloat(parts[1]);

        // Catch divide by zero
        if(denominator === 0) {
          return null;
        }
        return numerator/denominator;
      } catch(e) {
        return null;
      }
    } else {
      if(this.numberChecker(noUnits)) {
        try {
          return parseFloat(noUnits)
        } catch(e) {
          return null;
        }
      }
      return null;
    }
  };
1reaction
SaintPetercommented, Sep 18, 2020

I discovered another issue. Invalid input like a “double fraction”: https://metric-imperial-converter--freecodecamp.repl.co/api/convert?input=1//2gal

Causes a server error:

SyntaxError: Value expected (char 3)
    at createSyntaxError (/home/runner/Metric-Imperial-Converter/node_modules/mathjs/lib/expression/parse.js:1705:17)
    at parseEnd (/home/runner/Metric-Imperial-Converter/node_modules/mathjs/lib/expression/parse.js:1669:13)
    at parseParentheses (/home/runner/Metric-Imperial-Converter/node_modules/mathjs/lib/expression/parse.js:1655:12)
    at parseNumber (/home/runner/Metric-Imperial-Converter/node_modules/mathjs/lib/expression/parse.js:1626:12)
    at parseObject (/home/runner/Metric-Imperial-Converter/node_modules/mathjs/lib/expression/parse.js:1607:12)
    at parseMatrix (/home/runner/Metric-Imperial-Converter/node_modules/mathjs/lib/expression/parse.js:1532:12)
    at parseSingleQuotesString (/home/runner/Metric-Imperial-Converter/node_modules/mathjs/lib/expression/parse.js:1433:12)
    at parseDoubleQuotesString (/home/runner/Metric-Imperial-Converter/node_modules/mathjs/lib/expression/parse.js:1382:12)
    at parseSymbol (/home/runner/Metric-Imperial-Converter/node_modules/mathjs/lib/expression/parse.js:1270:12)
    at parseCustomNodes (/home/runner/Metric-Imperial-Converter/node_modules/mathjs/lib/expression/parse.js:1239:12)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Metric Imperial Converter - Quality Assurance Projects
This is a full walkthrough for the Metric Imperial Converter project on freeCodeCamp. We implement some functions to allow conversion of ...
Read more >
4 Test not passing in Quality Assurance Projects - JavaScript
log the values concerning the 4 test I'm not passing, it is logging the correct values. But, my code is not passing 4...
Read more >
Solved C+++ I NEED CODE, Flowchart/pseudo code as well
Question: C+++ I NEED CODE, Flowchart/pseudo code as well Concepts tested in this project [ALL concepts from previous projects] Selection control structure ...
Read more >
Metric/Imperial Conversion Errors - Chemistry LibreTexts
As the four examples below can attest, small errors in these unit systems can harbor massive ramifications. The Mars Climate Orbiter: A ...
Read more >
COLORADO METRIC CONVERSION MANUAL
be physically modified to be used in a metric project. ... resource management and inventory, survey control, engineering projects, Geographic.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found