11th Jul 2017, 8:09 AM
Ishaq Za'rour
9 odpowiedzi
+ 12
Just made a demo, there are anyway different ways to do the same thing and i tried to choose the more readable, try to experiment on your own the others... or why not? Make some controls. What if i add a character "a" rather than a number? Have fun! :) https://code.sololearn.com/WMG9EWqqG713/?ref=app
11th Jul 2017, 8:40 AM
Maz
Maz - avatar
+ 11
You called in a bad way the function at the onclick (you forgot to insert the parenthesis) and you make a calculation between two DOM elements and not their value. Basically this is a way to solve pretty easy: function plus(){ var num1=document.getElementById("num1").value; var num2=document.getElementById("num2").value; return parseInt(num1) + parseInt(num2); } function answer(){ alert (plus()); } In the first lines you obtain id's value and in the return value you make an addition between their value converting it in an integer number.
11th Jul 2017, 8:34 AM
Maz
Maz - avatar
+ 10
I edited, now it is more clear i hope! ^^
11th Jul 2017, 8:36 AM
Maz
Maz - avatar
+ 3
@Coder parseInt is a JavaScript method for type conversion, specifically for converting strings to Integers... it is used like this : var myNum = parseInt("100"); // this outputs 100. Another method for similar type conversion is parseFloat(); for converting strings to Floats...
11th Jul 2017, 9:01 AM
Aina Oluwafemi
Aina Oluwafemi - avatar
0
Just tell me what is missing
11th Jul 2017, 8:11 AM
Ishaq Za'rour
0
What does parseint do
11th Jul 2017, 8:35 AM
Ishaq Za'rour
0
Thx
11th Jul 2017, 8:36 AM
Ishaq Za'rour
0
It doesnt work
11th Jul 2017, 8:39 AM
Ishaq Za'rour
0
Thx ure awesome maz
11th Jul 2017, 8:41 AM
Ishaq Za'rour