Write a method that takes in a number and returns a string, placing a single dash before and after each odd digit. There is one exception: don’t start or end the string with a dash.
You may wish to use the `%` modulo operation; you can see if a number
is even if it has zero remainder when divided by two.
My approach to solving this problem:
- Convert given number to a string of numbers. For example, 203 => “2 0 3”
- Convert each number in the new string to integer for boolean comparisons.
- Go through the new array of numbers with a while loop
- Set the number at the first index (idx = 0) as current digit. (“2”)
- After index 0, if the previous digit or current digit is odd, add a dash “-“back to the new result and increment idx to go through next number in the array. (“20-3”)
Second time solving this:
- I forgot to convert the number back to integer for boolean.
- Also didn’t know to add the numbers and dashes back into the string correctly.
- Very annoyed in myself for not being able to solve this.