Building calculator | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

Building calculator

I'm building a calculator. There's an array to store values. I want if "." is clicked and displayed then after that it cannot be clicked/displayed again. const arr =[] ; if(arr.join("").replace(/[^. ]/g,"").length>1){ } With this if statement I'm able to check if the "." has been clicked again.. But i don't know what to write inside the statement so that it won't be added to array.

15th Jul 2021, 9:35 AM
dheeraj gupta
dheeraj gupta - avatar
6 Answers
+ 2
to avoid multiple decimal points use: //dealing with decimals $('#decimal').click(function () { var last = arr.join('').match(/\d+\.?\d*$/); if (!last) arr.push("0."); else if (/^\d+$/.test(last)) arr.push("."); $('#display').text(arr.join('')); }
26th Jul 2021, 3:23 PM
visph
visph - avatar
+ 1
dheeraj gupta giving your whole code would help to answer you (at least the related function)... I guess that you do your check after having still added the dot to your array, as you check for length strictly greater than one (so condition is true if at least two dots in the array) ? also you say "there is an array to store values", but we don't know what kind of data it is supposed to store: character typed by user? tokens entered by user? whole expression to be calculated or partial value to be displayed? without having a better idea of your code contexr, it's hard (if not impossible) to say to you "what to write inside the statement", or if you have to write something elsewhere or even if you better have to write it differently ^^ providing a link to your code playground project would definitively be the best option ;P
15th Jul 2021, 4:29 PM
visph
visph - avatar
0
Then just check for the opposite of it.
15th Jul 2021, 9:54 AM
Abhay
Abhay - avatar
0
Abhay How?
15th Jul 2021, 11:16 AM
dheeraj gupta
dheeraj gupta - avatar
0
dheeraj gupta If(! arr.join(""). replace(/[^.]/g, "").length>1) And do something now.
15th Jul 2021, 11:19 AM
Abhay
Abhay - avatar
0
Hey visph, here's my code https://code.sololearn.com/Wa8A123A50A2/?ref=app The problem is with the decimal. Its displaying multiple decimal like this 22.225.475.545.. While it should be only 22.225475545
16th Jul 2021, 7:46 AM
dheeraj gupta
dheeraj gupta - avatar