Split by variable separators

I’m in trouble :joy: how can I fix my appendJustOneDotPerNumber function?

what is the logic of that function? it would be much easier to help you if you talk us through it, instead letting us debug it without knowing nothing

Ok ok don’t get mad :rofl:

You are right :innocent:

The appendJustOneDotPerNumber function is triggered by the button click event with value “.” . Its job should be append one and only one dot per number. Since the expression to evaluate is a string, you can identify a number throught four predetermined separators (the “+” “-” “*” “/” math operators). At the moment it works well by using only one separator.

I’m new to the world of programming, please let me know if I can be more specific :slightly_smiling_face:

so the issue is that you are using only the + to split on here?
let splittedCurrentInput = input.split("+");
you can use a regular expression as argument of split

I would also say that you can do without this:

    for (let index = 0; index < splittedCurrentInput.length; index++) {
      inputPiece = splittedCurrentInput[index];

what’s the last value of index? just use that to assign inputPiece

you can use a regular expression as argument of split

yeah, this was my fear

are there other possibilities?

I don’t think so - why do you fear regular expressions?
you can just use the OR operator inside the regex, or a character class

It seems like an esoteric thing to me lol, sooner or later I will have to study them but I wanted to postpone just a little longer lmao

I had found a nasty way around the problem
and initially I just wanted help figuring out what was wrong with let splittedCurrentInput = input.split("+").join("-").split("*").join("/");

you are replacing + with - and * with \
what you wanted to do there?

you need the first three challenges of the fcc curriculum on regex to have a regex to use as argument of split

you are replacing + with - and * with \
what you wanted to do there?

have you tested it?

No, I hadn’t, but that’s what split and join used like that do. Did you test it?

Here my test:


sorry I meant have you tried the behavior?

what behaviour? I don’t understand what you are referring to

the behaviour of my calculator lol

yes, if you use a different operator than + the dot is not added
here was my answer

the behaviour of my calculator with let splittedCurrentInput = input.split("+").join("-").split("*").join("/");

beacause it seemed to me that it was enough to disable the possibility of adding an additional dot at the end of the number

i asked you if you’ve tried because that nasty feature works, believe it or not lmao

have you understand my issue now?

Come on don’t worry, it’s definitely my fault cause I can’t explain myself well in english.
I keep my curiosity.
However I solved with the regex, thanks a ton for the tips.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.