1

today I decided I wanted to make a simple js code that would accept a number (in meters), and decide what the appropriate metric unit to use would be. The code turned out to be a little more complicated than I had expected, but I was able to figure out most of the bugs as I found them (even if it meant rearranging all of my code). However, when it came to my if/else statement I could not figure it out. If I put in a number that is less than 1 nothing happens. If I put in a number more than 9 it logs the same thing every time. The structure itself may need some work, but if someone could help me with the if/else statement I would be very thankful. Here is the code (init is called when the body loads):

function init() {
            var x = prompt("How many meters?");
            convertMetricMeters(x);

            function convertDown(x) {
                if (0.1 >= x >= 0.99) {
                    console.log("deci");
                }

                else if (0.100 >= x >= 0.999) {
                    console.log("centi");
                }

                else if (0.1000 >= x) {
                    console.log("milli");
                }

                else {
                    console.log("error");
                }   
            }               

            function convertUp(x) {
                if (1 <= x <= 99) {
                    console.log("deca");
                }

                else if (100 <= x <= 999) {
                    console.log("hecto");
                }

                else if (1000 <= x) {
                    console.log("kilo");
                }

                else {
                    console.log("error");
                }
            }

            function convertMetricMeters(x) {
                if (x < 1) {
                    convertDown(x);
                }

                else if (x > 9) {
                    convertUp(x);
                }

                else {
                    console.log("Appropriate Metric Unit");
                }
            }
        }
revanchist
  • 63
  • 1
  • 1
  • 4

4 Answers4

1

Use && as AND operator in javascript

Convert these 100 <= x <= 999 to 100 <= x && x <= 999

1

You could simplify the check a bit and return if a condition is true.

function convertDown(x) {
    if (x < 0.01) {
        console.log("milli");
        return;
    }
    if (x < 0.1) {
        console.log("centi");
        return;
    }
    if (x < 1) {
        console.log("deci");
        return;
    }
    console.log("error");
}             
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

Your code has 2 sort of errors. One was simple to fix, that you have to add && between two conditions in if statement.

Now coming to the other part, the less than 1 items. It needed a different logic. Well, your maths seems to be needing bit attention. 0.1 is same as 0.100 and is same as 0.1000

I have updated the code to look for the number of digits after the decimal point and then console.log accordingly.

The updated code will be:

  function init() {
        var x = prompt("How many meters?");
        convertMetricMeters(x);

        function convertDown(x) {

            // checks the number of digits after decimal point
            decimals = (x.split('.')[1] || []).length

            if (decimals == 1 || decimals == 2) {
                console.log("deci");
            }

            else if (decimals == 3) {
                console.log("centi");
            }

            else if (decimals == 4) {
                console.log("milli");
            }

            else {
                console.log("error");
            }   
        }               

        function convertUp(x) {
            if (1 <= x && x <= 99) {
                console.log("deca");
            }

            else if (100 <= x && x <= 999) {
                console.log("hecto");
            }

            else if (1000 <= x) {
                console.log("kilo");
            }

            else {
                console.log("error");
            }
        }

        function convertMetricMeters(x) {
            if (x < 1) {
                convertDown(x);
            }

            else if (x > 9) {
                convertUp(x);
            }

            else {
                console.log("Appropriate Metric Unit");
            }
        }
    }

Working jsfiddle example: https://jsfiddle.net/w7pf3moL/

Nabeel Khan
  • 3,715
  • 2
  • 24
  • 37
0

A simplified version with only 1 method convert(float x):

function init() {
  var x = prompt("How many meters?");
  convertMetricMeters(x);
  function convert(x) {
    if (x < 0.01) console.log("milli");
    else if (x < 0.1) console.log("centi");
    else if (x < 1) console.log("deci");
    else if (x < 10) console.log("meter");
    else if (x < 100) console.log("deca");
    else if (x < 1000) console.log("hecto");
    else console.log("kilo");
  }
  function convertMetricMeters(x) {
    if (x > 0) {
      convert(x);
    } else {
      console.log("Appropriate Metric Unit");
    }
  }
}
init();
tklg
  • 2,572
  • 2
  • 19
  • 24