← Back to challenges

String Incrementer

JavaScriptHardstringsarrays

Instructions

Write a function which increments a string to create a new string.

  • If the string ends with a number, the number should be incremented by 1.
  • If the string doesn't end with a number, 1 should be added to the new string.
  • If the number has leading zeros, the amount of digits should be considered.

Examples

incrementString("foo") ➞ "foo1"

incrementString("foobar0009") ➞ "foobar0010"

incrementString("foo099") ➞ "foo100"

Notes

N/A

javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.