# Prevent double symbol in a string

**URL:** <https://forum.freecodecamp.org/t/prevent-double-symbol-in-a-string/167049>\
**Category:** General\
**Created:** [January 5, 2018, 10:52pm UTC](https://forum.freecodecamp.org/t/prevent-double-symbol-in-a-string/167049 "2018-01-05T22:52:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![stefalber](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/stefalber/32/73054_2.png) [@stefalber](https://forum.freecodecamp.org/u/stefalber)\
**Post date:** [January 5, 2018, 10:52pm UTC](https://forum.freecodecamp.org/t/prevent-double-symbol-in-a-string/167049/1 "2018-01-05T22:52:00Z")

</div>

Hi campers!  
I’m working on my Calculator and i want prevent this ![myCalculator](https://global.discourse-cdn.com/freecodecamp/original/3X/b/8/b8d837b1c9de8cb8c385cea9db80decbee1130fb.PNG) I tried to use some `replace` `split` trick, but no one worked with me. Does someone gives me some tips?  
Here my project:

> **[Calcul3tor](https://codepen.io/stefalber/full/KZvobz/)**
>
> ...

---

<div class="post-metadata">

**Author:** ![kevinSmith](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevinsmith/32/72934_2.png) [@kevinSmith](https://forum.freecodecamp.org/u/kevinSmith)\
**Post date:** [January 5, 2018, 11:02pm UTC](https://forum.freecodecamp.org/t/prevent-double-symbol-in-a-string/167049/2 "2018-01-05T23:02:27Z")

</div>

If I remembered correctly, I just checked when the decimal key was pressed to see if there was already a decimal in the input string.

---

<div class="post-metadata">

**Author:** ![stefalber](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/stefalber/32/73054_2.png) [@stefalber](https://forum.freecodecamp.org/u/stefalber)\
**Post date:** [January 5, 2018, 11:05pm UTC](https://forum.freecodecamp.org/t/prevent-double-symbol-in-a-string/167049/3 "2018-01-05T23:05:54Z")

</div>

@kevinSmith  
If you check the entire string you can put just one decimal at time. Because string like this “9.334 + 4.55” has no problem but it has 2 decimal.

---

<div class="post-metadata">

**Author:** ![kevinSmith](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevinsmith/32/72934_2.png) [@kevinSmith](https://forum.freecodecamp.org/u/kevinSmith)\
**Post date:** [January 5, 2018, 11:31pm UTC](https://forum.freecodecamp.org/t/prevent-double-symbol-in-a-string/167049/5 "2018-01-05T23:31:04Z")

</div>

Sorry, I was doing mine differently, pushing the number onto the stack when an operation was pushed, so there was only one number on the screen at a time.

If I were doing it you’re way, I might parse out the right most number of the string and check that. Another way to do it would be to work from right to left through the string and count the number of decimal points you get before you get a non-number/non-decimal. Or as Randell says, they’re probably some super-sexy reg ex solution.

---

<div class="post-metadata">

**Author:** ![abalducci](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/abalducci/32/78525_2.png) [@abalducci](https://forum.freecodecamp.org/u/abalducci)\
**Post date:** [January 5, 2018, 11:34pm UTC](https://forum.freecodecamp.org/t/prevent-double-symbol-in-a-string/167049/6 "2018-01-05T23:34:55Z")

</div>

Another relatively simple solution for this is to set a boolean ‘flag’ condition/variable that goes ‘true’ once a decimal has been used for a specific number term.

If the decimal has been used once, any other attempts to add a second one will just be ignored. Every time an ‘operation’ (i.e. \* / + -, etc) is used though the flag gets ‘reset’ to false. This should also allow you to have multiple terms with decimals on the display at once as shown.

---

<div class="post-metadata">

**Author:** ![stefalber](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/stefalber/32/73054_2.png) [@stefalber](https://forum.freecodecamp.org/u/stefalber)\
**Post date:** [January 5, 2018, 11:43pm UTC](https://forum.freecodecamp.org/t/prevent-double-symbol-in-a-string/167049/7 "2018-01-05T23:43:45Z")

</div>

@abalducci I thought about this solution, but I ask to found somethink less forced. Maybe I was a bit snooty. 😛 I thought with a counter set to 0 and 1. I implemented this solution and works really good. Thank you!

@camperextraordinaire @kevinSmith I tried this  
`var splitted = $( "input" ).val().replace(/\//g," ").replace(/-/g," ").replace(/x/g," ").reaplce(/\+/g," ").split(" ")`;  
and then check:

```auto
if(splitted[splitted.length-1].indexOf(".")===-1){ 

```

and then _add “.” to input_. But this doesn’t work.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 17, 2021, 8:31pm UTC](https://forum.freecodecamp.org/t/prevent-double-symbol-in-a-string/167049/8 "2021-01-17T20:31:28Z")

</div>


