0

why can't this work?

Why it gives me 6 as an answer even if I type 33*2? Please help https://code.sololearn.com/Wo6GLzIBRB01/?ref=app

22nd Apr 2019, 1:36 AM
Glory222
Glory222 - avatar
4 Réponses
+ 2
The problem is in your equal() function. Let's take a look at it. Your first variable (second) is given by the value of the button whose id is "fis". This is 3. Then your second variable (secon) is given by the value of the button whose id is "seco". This is 2. Then your variable all is found my multiplying these two together (2*3 = 6), and this value of "all" is displayed on your calculator. All this means is that, it doesn't matter what buttons have been pressed before you press the "=" button, once that button is pressed and the equal() function is called, the same sequence will happen. The easiest way of fixing this is probably to do what I've done here: https://code.sololearn.com/WF7LS31e2020/?ref=app. Now this equal() function takes what is in your calculator display and evaluates it and prints the answer. Just a word of warning though: use of the eval() method is discouraged in general. It's ok for something simple like this, but if you use it when a user is able to input text, it leaves your code very vulnerable to attacks.
22nd Apr 2019, 8:47 AM
Russ
Russ - avatar
0
When you use the fis.value and seco.value you will get the text inside the button. Check the console in your code https://code.sololearn.com/WiTJcVIDto60/?ref=app
22nd Apr 2019, 2:05 AM
oh Common
oh Common - avatar
0
How do I fix it
22nd Apr 2019, 3:56 AM
Glory222
Glory222 - avatar
0
Thank you russ, I really didn't know and haven't heard of eval () method!! Now it works perfectly
22nd Apr 2019, 12:11 PM
Glory222
Glory222 - avatar