0

When it runs it simply wont return a value and i can't understand why. It seemed to work fine when i ran it in a html file.

Also, when number is 5 for example (number+number) has been returning 55 instead of 10. I thought it was because i had quotation marks but I've checked again and again and it's not.

Help much appreciated

HTML

<body>

<form id = 'table' name = 'table'>
<p> Gimme a number </p>
<input id = 'textbox' type = 'number' name = 'inputBox'>
<input id = 'button' type = 'button' value = 'Submit' onclick = 'check()'>

</form>
</body>

Javascript

function check(){
    var number = document.table.inputBox.value;
var array = [number];
array.push(number+number);
alert(array);

https://jsfiddle.net/67tt6te7/

Nick
  • 1
  • Read the string, parse it as an integer: `var number = parseInt(document.table.inputBox.value, 10);` – Alex K. Jun 20 '17 at 13:46
  • Your fiddle does not work because your fiddle is set up wrong. The JavaScript is not in global scope. Click the gear icon and change the load type to be body or head. – epascarello Jun 20 '17 at 13:46

1 Answers1

1

its returning 55 because its telling what you ask it its adding 2 strings to each other "5"+"5" is "55"

If you want to sum their numeric values you need to use parseInt function to convert them into number first

parseInt(number)+parseInt(number) will give you 10

nikoss
  • 3,254
  • 2
  • 26
  • 40