lundi 30 mars 2020

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>

Aucun commentaire:

Enregistrer un commentaire