practice-problems-1/comments/12-dasherize-number.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.
#
# Difficulty: medium.

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.

medium.
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 . medium .
def dasherize_number(num)
  nums = num.to_s.split('')     
  nums.map! do |i|              
    if i.to_i.odd? == true      
      i = "-"+ i                
    end
   i.to_s                       
  end
  if nums[0].to_i.odd?                       
    nums[0].reverse!                         
  end
nums = nums.join('').split('')                      
nums.map! do |k|                                    
   index = nums.index(k)                            
    if nums[index] == "-" && nums[index+1] == "-"   
        nums.delete_at(index)                       
    end
    k = k                                           
 end
return nums.join('')                             
end
def dasherize_number ( num ) nums = num . to_s . split ( '' ) nums . map! do | i | if i . to_i . odd? == true i = "-" + i end i . to_s end if nums [ 0 ] . to_i . odd? nums [ 0 ] . reverse! end nums = nums . join ( '' ) . split ( '' ) nums . map! do | k | index = nums . index ( k ) if nums [ index ] == "-" && nums [ index + 1 ] == "-" nums . delete_at ( index ) end k = k end return nums . join ( '' ) 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 .
#turns argument into a string and splits it into an array of single numbers. NOTE: '2' != 2
 #Loop through nums .map! so you can change the orginal array
 # convert each iteration from string to integer, if its odd...
 # add a "-" before the number
 #leave it alone if its not odd and convert it back a string
 #^^^
 #this is to compensate for odd numbers at the beginning.  
 #example: ["-3","0", "-3"] >> ["3-","0", "-3"] 
 #converts ["-3","0", "-3"] >> ["3", "-", "0", "-", "3"].
 #loop through nums
 #set index
 # checks to see if a dash is next to a dash
 #if so, delete it
 # otherwise leave k alone
 #return as a single string

num
<not_specific>