简体   繁体   中英

How to create an if else statement for length units with javascript

My goal is to create a true mathematical statement with length units. The only part I am having trouble with, is the if else part of the Javascript. What should I change, so that my if else statement for length units works? This is what I have done so far:

 var operators = ['+', '-']; var e = ['km', 'm']; function F1() { num1 = document.getElementById("num1"); num2 = document.getElementById("num2"); rnum1 = Math.floor((Math.random() * 10) + 1); rnum2 = Math.floor((Math.random() * 10) + 1); num1.innerHTML = rnum1; num2.innerHTML = rnum2; oper = document.getElementById("operator"); op = operators[Math.floor(Math.random() * 2)]; oper.innerHTML = op; eht = document.getElementById("e1"); eht2 = document.getElementById("e2"); eh = e[Math.floor(Math.random() * e.length)]; eh2 = e[Math.floor(Math.random() * e.length)]; eht.innerHTML = eh; eht2.innerHTML = eh2; answer = document.getElementById("answer"); if (eh = 'km') { if (eh2 = 'm') { answer.innerHTML = eval(rnum1 * 1000 + op + rnum2); } else { if (eh = 'm') { if (eh2 = 'km') { answer.innerHTML = eval(rnum1 + op + rnum2 * 1000); } else { if (eh = 'km') { if (eh2 = 'km') { answer.innerHTML = eval(rnum1 * 1000 + op + rnum2 * 1000); } else { answer.innerHTML = eval(rnum1 + op + rnum2) }; } } } } }
 <p> <label id="num1"> </label> <label id="e1"> </label> <label id="operator"> </label> <label id="num2"> </label> <label id="e2"> </label> = <label id="answer">m </label> </p> <button onclick="F1()"> New </button>

I wish I understood the problem better but I updated your IF-ELSE logic as per Jaromanda X's observation.

= is only for assigning values and should not be used to check equality. Instead, you should use either == or === when trying to evaluate the equality of multiple values.

For more information on this concept, please refer to MDN Equality comparisons and sameness .

Below is an updated version of your code that compiles and runs but again, I'm not sure what exactly the expected result is so I will let you determine if it is working as expected now or not.

<script>
    var operators = ['+', '-'];
    var e = ['km', 'm'];

    function F1() {
        num1 = document.getElementById("num1");
        num2 = document.getElementById("num2");
        rnum1 = Math.floor((Math.random() * 10) + 1);
        rnum2 = Math.floor((Math.random() * 10) + 1);
        num1.innerHTML = rnum1;
        num2.innerHTML = rnum2;
        oper = document.getElementById("operator");
        op = operators[Math.floor(Math.random() * 2)];
        oper.innerHTML = op;
        eht = document.getElementById("e1");
        eht2 = document.getElementById("e2");
        eh = e[Math.floor(Math.random() * e.length)];
        eh2 = e[Math.floor(Math.random() * e.length)];
        eht.innerHTML = eh;
        eht2.innerHTML = eh2;
        answer = document.getElementById("answer");

        if (eh === 'km') {
            if (eh2 === 'm') {
                answer.innerHTML = eval(rnum1 * 1000 + op + rnum2);
            }

            else {
                if (eh === 'm') {
                    if (eh2 === 'km') {
                        answer.innerHTML = eval(rnum1 + op + rnum2 * 1000);
                    }

                    else {
                        if (eh === 'km') {
                            if (eh2 === 'km') {
                                answer.innerHTML = eval(rnum1 * 1000 + op + rnum2 * 1000);
                            }
                            else {
                                answer.innerHTML = eval(rnum1 + op + rnum2)
                            };
                        }
                    }
                }
            }
        }
    }
</script>

<p>
    <label id="num1"> </label> <label id="e1"> </label>

    <label id="operator"> </label>

    <label id="num2"> </label> <label id="e2"> </label>

    = <label id="answer">m </label>
</p>
<button onclick="F1()"> New </button>

I realised for what I was trying to create i didn't needed to add an else statement. So I replaced all if else statements with the code below and then it worked:

if (eh === 'km') 
    {if (eh2 === 'm') 
    {answer.innerHTML = eval(rnum1 * 1000 + op + rnum2);}}

if (eh === 'm') 
    {if (eh2 === 'km') 
    {answer.innerHTML = eval(rnum1 + op + rnum2 * 1000);}}

if (eh === 'km') 
    {if (eh2 === 'km') 
    {answer.innerHTML = eval(rnum1 * 1000 + op + rnum2 * 1000);}}

if (eh === 'm') 
    {if (eh2 === 'm') 
    {answer.innerHTML = eval(rnum1 + op + rnum2);}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM