How to remove occurrence of a character if that char already exists? | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
0

How to remove occurrence of a character if that char already exists?

For example if a "." is entered then another "." cannot be added in the string or an array

15th Jul 2021, 4:23 PM
dheeraj gupta
dheeraj gupta - avatar
4 ответов
+ 1
If a is an array and it already has an "." then I don't want another "." to be added in a.
15th Jul 2021, 5:24 PM
dheeraj gupta
dheeraj gupta - avatar
0
Check this code, Suppose a is original value and tmp is new value . Not that match method will check if there are more than one occurence of character "." in tmp value , if yes it won't do anything otherwise tmp(new) value will be assigned to original (previous) value. let a="abff.cghh" let tmp="abcf.hjjk." if(!(tmp.match(/\./g).length>1)){ a=tmp } console.log(a) The above code isn't removing but checking new value before adding to previous value . If that is what you don't want then let me know!
15th Jul 2021, 4:51 PM
Abhay
Abhay - avatar
0
dheeraj gupta a=["a",".","b"] if(!a.join("").match(/\./)){ // do something ! } the above if loop will work only if "." isn't in array.
15th Jul 2021, 5:37 PM
Abhay
Abhay - avatar
0
It won't do the work. Let me explain it briefly. I'm making a calculator. And this is about dealing with decimal values. I want to code the situation in which it will check if the array/string has a decimal or not, if it does have a decimal then it has to stop adding another decimal in the array/string. For example - 13.2222.2555.
15th Jul 2021, 6:50 PM
dheeraj gupta
dheeraj gupta - avatar