Skip to content
Advertisement

Incrementing specific numbers contained within a string

I’m working with strings that look like this:

JavaScript

I’d like to increment each number in the string past a specific point exempting numbers next to ‘/’ or ‘x’. For example, if I want to increment all the numbers of the aforementioned string past the point do3mi3so3, I’d expect to get this:

JavaScript

Here is my code:

JavaScript

Despite the numbers successfully incrementing, the value of the string does not change.

Thanks.

Advertisement

Answer

You can’t actually set the character of a string at a specific index using something like notes[i] = "a". What you can do is split the string into an array, change the values in that array, and then join it back together into a string when you’re done.

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement