Dasherize Number

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.

Screen Shot 2016-08-16 at 4.43.04 PM

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.

 

Advertisements
Dasherize Number

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s