I have made a calculator in which my % (Percentage) not work correctly for 100%50 or 100%20. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

I have made a calculator in which my % (Percentage) not work correctly for 100%50 or 100%20.

https://code.sololearn.com/W6e0OSA6XRk1/?ref=app

28th Jan 2023, 6:11 AM
CHIRAG RAJ
CHIRAG RAJ - avatar
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.
28th Jan 2023, 10:43 AM
PanicS
PanicS - avatar
+ 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"
28th Jan 2023, 9:12 AM
PanicS
PanicS - avatar
+ 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.
28th Jan 2023, 4:50 PM
Thakur Samar
Thakur Samar - avatar
+ 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
28th Jan 2023, 5:04 PM
Thakur Samar
Thakur Samar - avatar
+ 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 😐
28th Jan 2023, 6:17 AM
CHIRAG RAJ
CHIRAG RAJ - avatar
+ 2
Mirielle can you elaborate more
28th Jan 2023, 7:07 AM
CHIRAG RAJ
CHIRAG RAJ - avatar
+ 2
Why it is happening, can you please explain me
28th Jan 2023, 7:08 AM
CHIRAG RAJ
CHIRAG RAJ - avatar
+ 2
PanicS but, it not give wrong ans if we do 500%10 or 256%43 etc etc. Why it is so.
28th Jan 2023, 9:23 AM
CHIRAG RAJ
CHIRAG RAJ - avatar
+ 2
PanicS Thanku so much for your help
28th Jan 2023, 10:50 AM
CHIRAG RAJ
CHIRAG RAJ - avatar
+ 1
Okay 👍
28th Jan 2023, 2:28 PM
CHIRAG RAJ
CHIRAG RAJ - avatar
0
Ответ=0 . Так как 100:20=5 без остатка
29th Jan 2023, 3:03 PM
Алексей