simple-exercises/ruby-exercises/easy-1.rb
# 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.
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.
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 .
def dasherize_number(num)
  number = num.to_s
  dashed = ""
  dashed = number[0]
  if number[0].to_i % 2 == 1
    dashed += "-"
  end
  number[1..-1].each_char do |n|
    if n.to_i % 2 == 1
      if dashed[-1] != "-"
        dashed = dashed + "-" + n + "-"
      else
        dashed += n
      end
    else
      dashed += n
    end
  end
  if dashed[-1] == "-"
    return dashed[0...-1]
  end
  dashed
end
def dasherize_number ( num ) number = num . to_s dashed = "" dashed = number [ 0 ] if number [ 0 ] . to_i % 2 == 1 dashed += "-" end number [ 1 .. - 1 ] . each_char do | n | if n . to_i % 2 == 1 if dashed [ - 1 ] != "-" dashed = dashed + "-" + n + "-" else dashed += n end else dashed += n end end if dashed [ - 1 ] == "-" return dashed [ 0 ... - 1 ] end dashed end
Write a method that takes in a number and returns a string, placing
a single dash before and after each odd digit.
Write a method that takes in a number and returns a string placing a single dash before and after each odd digit .
num
<not_specific>