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 class="button">=</button>
<button class="button">C</button>
<button class="button">x</button>
<button class="button">÷</button>
<br/>
<button class="button">in</button>
<button class="button">Cm</button>
<br/>
<button class="button">1</button>
<button class="button">2</button>
<button class="button">3</button>
<br/>
<button class="button">4</button>
<button class="button">5</button>
<button class="button">6</button>
<br/>
<button class="button">7</button>
<button class="button">8</button>
<button class="button">9</button>
<br/>
<button class="button">0</button>
>Solution :
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;
}