Home > OS >  Why isn't my imperial to metric calculation function working?
Why isn't my imperial to metric calculation function working?

Time:12-16

I am building a webpage based scientific calculator, where it have some basic functions i.e. ,-,*,/. but I wanted to add Cm to Inch and Inch to Cm conversion in it too, and that's where I am having issues it is not performing these two functions only other than this all functions are working.

var display = document.getElementById("screen");
var buttons = document.getElementsByClassName("button");
  
Array.prototype.forEach.call(buttons, function(button) {
  button.addEventListener("click", function() {
    if (
    button.textContent != "=" && 
    button.textContent != "C" && 
    button.textContent != "x" && 
    button.textContent != "÷" &&
    button.textContent != "in"&&
    button.textContent != "Cm") {
      display.value  = button.textContent;
    } else if (button.textContent === "=") {
      equals();
    } else if (button.textContent === "C") {
      clear();
    } else if (button.textContent === "x") {
      multiply();
    } else if (button.textContent === "÷") {
      divide();
    } else if (button.textContent === "in") {
      LengthConverter_in();
    } else if (button.textContent === "Cm") {
      LengthConverter_Cm(valNum);
    }
  });
});

function equals() {
    display.value = eval(display.value)
    //checkLength()
    //syntaxError()
}

function clear() {
  display.value = "";
}

function backspace() {
  display.value = display.value.substring(0, display.value.length - 1);
}

function multiply() {
  display.value  = "*";
}
function LengthConverter_in() {
    display.value = value/0.39370;
  }
function LengthConverter_Cm() {
  display.value = value*0.39370;
}
<input id="screen"/>
<br/>
<button >=</button>
<button >C</button>
<button >x</button>
<button >÷</button>
<br/>
<button >in</button>
<button >Cm</button>
<br/>
<button >1</button>
<button >2</button>
<button >3</button>
<br/>
<button >4</button>
<button >5</button>
<button >6</button>
<br/>
<button >7</button>
<button >8</button>
<button >9</button>
<br/>
<button >0</button>

CodePudding user response:

In the two functions value is not defined, but you are using it. it needs to be changed to display.value

function LengthConverter_in() {
    display.value = display.value/0.39370;
}
function LengthConverter_Cm() {
  display.value = display.value*0.39370;
}
  • Related