Building calculator | Sololearn: Learn to code for FREE!


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.

7/15/2021 9:35:53 AM

dheeraj gupta

6 Answers

New Answer


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('')); }


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


Then just check for the opposite of it.


Abhay How?


dheeraj gupta If(! arr.join(""). replace(/[^.]/g, "").length>1) And do something now.


Hey visph, here's my code The problem is with the decimal. Its displaying multiple decimal like this 22.225.475.545.. While it should be only 22.225475545