0
 function = () => {
    var info1 = parseInt(document.getElementById("info1").value);
    var info2 = parseInt(document.getElementById("info2").value);
    var res = Number(info1.value + info2.value);
    var info3 = document.getElementById("info3");
    info3 = Number(res);
 }

Now I am getting the answer in alert but not in the info3. I am getting NaN as output in the third. I know its Not a Number thing but how to convert in so that I get output in my column.

1
  • 1
    function = () = {} is not a valid definition, btw. You need to either declare it as const a = () => {} or function a(){ } or const a = function(){} Commented Oct 18, 2019 at 8:37

2 Answers 2

2

info1 and info2 already has the value of the element. You are wrongly trying to access the value again which really does not exists and provides undefined. Thus undefined + undefined results NaN (Not-A-Number).

Also, info3 refers to the element, you have to use the value property to assign the new value to the element.

Please Note: function = () => {... is not the valid syntax. Also, as the input value is of type string, converting the value to number before setting the value is meaningless.

Try the following way:

var myFunction = () => {
  var info1 = parseInt(document.getElementById("info1").value);
  var info2 = parseInt(document.getElementById("info2").value);
  var res = info1 + info2;
  var info3 = document.getElementById("info3");
  info3.value = res; // Number(res) is meaningless here
}
Sign up to request clarification or add additional context in comments.

Comments

0

If it's return NaN make it 0

example var info1= NaN|0

function = () => {
    var info1 = parseInt(document.getElementById("info1").value) | 0;
    var info2 = parseInt(document.getElementById("info2").value) | 0;
    var res = Number(info1.value + info2.value) | 0;
    var info3 = document.getElementById("info3");
    info3 = Number(res) | 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.