Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

291
Views
HTML, JS calculator output number as NaN

I was trying to create a simple demo calculator and I got the output as NaN. I think it is something with the JS calling the input values. Please refer to the below code.

var num1 = document.getElementsByName(firstNoInput).value;
var num2 = document.getElementsByName(secondNoInput).value;
var ans;

function addition() {
  ans = num1 + num2;
  document.getElementById("screen").innerHTML = ans;
}

function subtraction() {
  ans = num1 - num2;
  document.getElementById("screen").innerHTML = ans;
}

function multiplication() {
  ans = num1 * num2;
  document.getElementById("screen").innerHTML = ans;
}

function division() {
  ans = num1 / num2;
  document.getElementById("screen").innerHTML = ans;
}
<!DOCTYPE html>
<html>

<head>
  <title>Calculator</title>
</head>

<body>
  <div>
    <header id="screen"></header>
    <table>
      <tbody>
        <tr>
          <td>First Number:</td>
          <td name="firstNoInput"><input type="text" /></td>
        </tr>
        <tr>
          <td>Second Number:</td>
          <td name="secondNoInput"><input type="text" /></td>
        </tr>
        <tr>
          <td><button type="submit" onclick="addition()">+</button></td>
          <td><button type="submit" onclick="subtraction()">-</button></td>
        </tr>
        <tr>
          <td><button type="submit" onclick="multiplication()">*</button></td>
          <td><button type="submit" onclick="division()">/</button></td>
        </tr>
      </tbody>
    </table>
  </div>

</body>

</html>

I even used the following code (convert values into strings), but it didn't work.

var num1 = parseInt(document.getElementsByName(firstNoInput).value);
var num2 = parseInt(document.getElementsByName(secondNoInput).value);

Can anyone help?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

var num1 = document.getElementsByName(firstNoInput).value;
var num2 = document.getElementsByName(secondNoInput).value;

is wrong. You should pass the parameters as strings. However, ignore this part, since the both variables that you define make only sense if your input fields already have values when the page gets loaded. Since you do not have these values, you will need a key listener that gets you the values whenever the user types.

Also, as described in the comments, getElementsByName() returns a NodeList and not an element.

let num1;
let num2;
document.getElementsByName('firstNoInput')[0].addEventListener('keyup', (e) => {
  num1 = e.target.value;
});
document.getElementsByName('secondNoInput')[0].addEventListener('keyup', (e) => {
  num2 = e.target.value;
});


function addition() {
  ans = Number(num1) + Number(num2);
  document.getElementById("screen").innerHTML = ans;
}

function subtraction() {
  ans = Number(num1) - Number(num2);
  document.getElementById("screen").innerHTML = ans;
}

function multiplication() {
  ans = Number(num1) * Number(num2);
  document.getElementById("screen").innerHTML = ans;
}

function division() {
  ans = Number(num1) / Number(num2);
  document.getElementById("screen").innerHTML = ans;
}
<header id="screen"></header>
<table>
  <tbody>
    <tr>
      <td>First Number:</td>
      <td name="firstNoInput"><input type="text" /></td>
    </tr>
    <tr>
      <td>Second Number:</td>
      <td name="secondNoInput"><input type="text" /></td>
    </tr>
    <tr>
      <td><button type="submit" onclick="addition()">+</button></td>
      <td><button type="submit" onclick="subtraction()">-</button></td>
    </tr>
    <tr>
      <td><button type="submit" onclick="multiplication()">*</button></td>
      <td><button type="submit" onclick="division()">/</button></td>
    </tr>
  </tbody>
</table>

about 4 years ago · Juan Pablo Isaza Report

0

Your var num1 and num2 are being set only when you start the script. Since it looks like you would like to use those as variables for your calculations, we can use a getter function.

And you can use a setter function on the output.

// var num1 = document.getElementsByName(firstNoInput).value; 
// not the input! this is the td element.
// var num2 = document.getElementsByName(secondNoInput).value;

var num1 = { get value() { 
   return (document.getElementById("number1").value * 1); 
   // note multiply by 1 to make typeof number.
} };
var num2 = { get value() { 
   return (document.getElementById("number2").value * 1); 
   // note multiply by 1 to make typeof number.
} };

var ans = { set value(z) {        
   document.getElementById("answer").innerHTML = z} 
};



function addition() {
  ans.value = num1.value + num2.value;
  // note num1.value and num2.value must be typeof number.
}

function subtraction() {
  ans.value = num1.value - num2.value;
}

function multiplication() {
  ans.value = num1.value * num2.value;
}

function division() {
  ans.value = num1.value / num2.value;
}
<!DOCTYPE html>
<html>

<head>
  <title>Calculator</title>
</head>

<body>
  <div>
    <header id="screen"></header>
    <table>
      <tbody>
        <tr>
          <td>First Number:</td>
          <td name="firstNoInput"><input id="number1" type="text" value=""/></td>
        </tr>
        <tr>
          <td>Second Number:</td>
          <td name="secondNoInput"><input id="number2" type="text" value="" /></td>
        </tr>
        <tr>
          <td>Answer</td>
          <td id="answer"></td>
        </tr>  
        <tr>
          <td><button type="submit" onclick="addition()">+</button></td>
          <td><button type="submit" onclick="subtraction()">-</button></td>
        </tr>
        <tr>
          <td><button type="submit" onclick="multiplication()">*</button></td>
          <td><button type="submit" onclick="division()">/</button></td>
        </tr>
      </tbody>
    </table>
  </div>
  </body>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!