+ 1

i cant make javascript calculator when i use + sign it just connect two numbers

like 1+1 =11

15th Dec 2016, 5:03 PM
Salman Shaikh
Salman Shaikh - avatar
6 Answers
+ 3
It's because the plus sign can mean addition as well as concatenation. A trick that I use is to multiply the "string" by 1 to make sure it turns into an integer. If you have x to be "1" and y to be "1", you can do z = x*1 + y*1 and you will get 2 as the result.
16th Dec 2016, 2:03 AM
David Sebastian Keshvi Illiakis
David Sebastian Keshvi Illiakis - avatar
+ 2
I think that you are trying to add two strings each other ( actually i'm sure that what you're doing) That is a mistake that happen all the time. "1" + "1" = "11" 1 + 1 = 2 If the number that you try to add each other came from on input, they are strings, not numbers. In your code, try to use the fonction Number(). Like this --> // x and y represent your number, which re actually strings.. Number(x) + Number(y). Number() function take a string as a parameter and return an number.
15th Dec 2016, 5:11 PM
Alexandre Cbt
Alexandre Cbt - avatar
+ 1
You need to use parent for that. example: if you want to add 2 and 3 then var k= parseInt (2,10) + parseInt (3,10) now k will contain value 5 (2+3) 10 is radix (base of number system)
15th Dec 2016, 5:10 PM
Viral Parekh
Viral Parekh - avatar
+ 1
var operator = prompt("What do you want to do?") var Number(1)= prompt (7) var Number(2) = prompt (8) if ("operator = *") { var ans = value1 * value2 } if ("operator = /") { var ans = value1 / value2 } if ("operator = -") { var ans = value1 - value2 } alert(ans);
15th Dec 2016, 5:15 PM
Salman Shaikh
Salman Shaikh - avatar
+ 1
this is my program
15th Dec 2016, 5:16 PM
Salman Shaikh
Salman Shaikh - avatar
0
I can't understand can you give detailed ans
15th Dec 2016, 5:11 PM
Salman Shaikh
Salman Shaikh - avatar