Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading