Free webinar! How to Make Generative AI Work for You
Get my free ticket+ 2
I have made a calculator in which my % (Percentage) not work correctly for 100%50 or 100%20.
11 Answers
+ 2
For 100%50, 100%20 and 9/3(my test) it work correctly but it look like nothing change, result is 50 and 20, and than if you click equal again you concatenate first number to result so it do:
"100" + "50" = "10050"
Your calculator work fine but you miss logic when user click again at equal, now it concatenate first number with result, you should handle this case, you may reset input to be blank, or to first number be result, so user can continue to calculate, it is up to you.
So only bug I found is when i click equal in sequence. You should make some style different so it is clear for user that calculator do something.
Here is some ideas:
https://www.calculator.net/
Here they place full calculation in one line: 1 + 1 = and in new line we get result (it also have blue background so it is visually diferent)
Also check google calculator, what show when you type "calculator"
I expected to see change same as you so we clicked again at equal 😄 Thats why it look like it work for some number but not for all.
+ 4
As Mirielle said you have some problem with string concatenation, this means data you need to calculate are string format like this "9" not number 9, so if you do 9/3 you got 93, "9" is probably read from screen input and 3 is from calculation so it do "9" + "3" what is "93".
This also show another problem, why would you even add first number to divide result, you should clear input and show only result.
To fix string concatination:
You should convert data from input what are always string format to number (make sure you does not have some message or NaN as value because it can break code)
Mostly we use parseInt()
Here are all methods you can use to convert string to number and how to use them:
https://www.freecodecamp.org/news/how-to-convert-a-string-to-a-number-in-javascript/amp/
As I said make sure data you are converting are number inside string not NaN or something else like infinity or custom message you wanna send like "you can't divide by 0"
+ 3
CHIRAG RAJ Your problem lies on line 123 your code works fine when "=" is pressed once but if pressed again causes the strings to concatenate due to line 123 and your "show" becomes 10050 for 100%50.
+ 3
https://code.sololearn.com/WmTWR6HeG2jM/?ref=app
Try this CHIRAG RAJ and see var count at 70,77,127,129
This might fix your bug
+ 2
It will not work correctly for "/" divide as well but only for few opened like 100/10 or 100%10 or 100% 30 etc and it will work fine for all other operand. Please anyone help 😐
+ 2
Mirielle can you elaborate more
+ 2
Why it is happening, can you please explain me
+ 2
PanicS but, it not give wrong ans if we do 500%10 or 256%43 etc etc. Why it is so.
+ 2
PanicS Thanku so much for your help
+ 1
Okay 👍
0
Ответ=0 . Так как 100:20=5 без остатка