+ 1

What is wrong with my code?

Here is the html code: <!DOCTYPE html> <html> <head> </head> <body> <h1 id="result" align="center">0</h1> <button onclick="operation('+')">+</button> <button onclick="number(1)">1</button> <button onclick="number(2)">2</button> <button onclick="number(3)">3</button> <br> <button onclick="operation('-')">-</button> <button onclick="number(4)">4</button> <button onclick="number(5)">5</button> <button onclick="number(6)">6</button> <br> <button onclick="operation('*')">*</button> <button onclick="number(7)">7</button> <button onclick="number(8)">8</button> <button onclick="number(9)">9</button> <br> <button onclick="operation('/')">/</button> <button onclick="point()">.</button> <button onclick="number(0)">0</button> <button onclick="equals()">=</button> </body> </html> And here is the Javacript code: var eq=0; var x; var y; var a=0; var b=0; var c=0; var first=0; var second=0; var oper; var res; function number(x) { if(b===0) { if (a===0) { first=x; a++; eq=first; } else { first+=String(x); eq+=String(first); } } else { if (c===0) { second=x; c++; } else { second+=String(x); } eq+=second; } } function operation(y) { oper=String(y); eq+=String(y); b++; } function equals() { switch(oper) { case '+': res=Number(first)+Number(second); break; case '-': res=Number(first)-Number(second); break; case '*': res=Number(first)*Number(second); break; case '/': res=Number(first)/Number(second); break; } eq+= "=" + res; } setInterval(update, 1); function update() { document.getElementById("result").innerHTML = eq; }

4th Nov 2017, 1:35 AM
Joe Shanahan
Joe Shanahan - avatar
8 Answers
+ 6
1) Please specify, are you getting any errors? 2) đŸ˜± PLEASE PLEASE PLEASE use the Code Playground.
4th Nov 2017, 3:29 AM
Learnsolo
+ 4
meaning what works not?
4th Nov 2017, 1:38 AM
Abdur-Rahmaan Janhangeer
Abdur-Rahmaan Janhangeer - avatar
+ 3
Technically, there's nothing wrong in your code (no errors, no unexpected behaviour regarding the code), but you probably don't get the expected behaviour as you want: without describing what you're trying to obtains and what's the problem you encounter, we cannot help you accuratly... Anyway, I think you could think to things such as case when '=' is pressed first (actually output: "0=undefined") and/or case when '=' was pressed once, next buttons pressingshould have behaviour depending on wich button, and what's actual result (digit should reset output with new digit value, operator should be appended to result value only, and so on). Also, you could add some delete/reset feature?
4th Nov 2017, 10:22 AM
visph
visph - avatar
+ 3
It's hard to explain why, but mainly your logic is more complex than necessary ^^ (Also, you will be advised to use more explicit name for your variables -- or at least write comments to explain their use: 'a', 'b', and 'c' are not obvious to determine what's their use ;P) In your 'number()' function: > if b==0: (calculator expect 'first' number -- argument for calculator -- to be inputed) > if a==0: (there's no value still set for 'first' number -- argument for calculator) + 'first' is set with the digit passed at argument ('x') + 'a' is incremented (so a is now != 0) + 'res' is set with 'first' value > else: (a!=0, digit(s) are already stored in 'first') + 'first' is appended with the actual typed (new) digit ('x') + 'res' is appended with the updated value of 'first' This last line is your first problem... try to see what's happening with real values: > a first digit is clicked (number(4) is called), so now: > a == 0 so: + 'first' is set to 4 ('x', first==4) + 'a' is incremented (a==1) + 'eq' is set to 'first' (eq==4) > a second digit is clicked (number(2) is called, b still equals 0), so now: > a !=0 so: + 'first' is appended with 2 ('x', first==4+'2'=='42') + 'eq' is appended with 'first' (eq==4+'42'=='442') The solution, is to not append 'eq' with 'first', but assign 'eq' with same value than stored in 'first'... do: eq = first; instead of: eq += first; That's for the (b==0) case... (I will continue on another post, because of post length limitation)
5th Nov 2017, 6:19 AM
visph
visph - avatar
+ 3
(second part of explanations) Next, we have same kind of problem when (b!=0 -- operator already entered by user, 'second' number/argument expected), but we cannot correct by same way, as if we update 'res' with 'second' we will lost the first part of expression ('first' value and operator): solution now, is to update 'res' value with all expression rebuild each time a digit is clicked... do: eq=first+oper+second; instead of: eq+=second; That's enough to sole the problem asked... but anyway, there are still some case you should integrate in your code (as I've mentionned in my very first post -- previously to the first part of this one), as what's happening when digit is clicked after result was computed? 'a', 'b', 'c' flags variables are now both different of 0, so this should not work as expected ^^ Simplest fix, is to assume that new digits clicked start a new calcul, so reset your 3 flag variables at end of 'equals()' function: a = 0; b = 0; c = 0; In same way, you could update your code to handle cases of operator clicked more than once, or after second number started to be typed ;) As comparison, check my calculator code: it's more advanced, so it's probably a little harder to read, but could inspire you in the alternative way you could follow to implement a calculator: https://code.sololearn.com/WtF2wev8Vc4m/?ref=app
5th Nov 2017, 6:20 AM
visph
visph - avatar
+ 2
I very very love SL! Thx thx thx! But my sad: that outlets of the SL Python mobile interpretator isn't a text for copypast.
11th Nov 2017, 4:02 AM
Vitalij Bredun ♡ Python Petersburg
Vitalij Bredun ♡ Python Petersburg - avatar
0
Oh, sorry, I am having a problem where it enters the wrong numbers into the calculator and I don't know why. Also I have the code public on my account, it is called Click Calculator(work in progress)
5th Nov 2017, 12:23 AM
Joe Shanahan
Joe Shanahan - avatar
0
It works now, thank you
7th Nov 2017, 5:05 PM
Joe Shanahan
Joe Shanahan - avatar