FizzBuzz in Ruby - Women Who Code.
Resources for women who code
women who code, girls who code, ladies who code
52053
post-template-default,single,single-post,postid-52053,single-format-standard,qode-core-1.0.1,ajax_fade,page_not_loaded,,capri child-child-ver-1.0.0,capri-ver-1.6, vertical_menu_with_scroll,smooth_scroll,grid_1200,blog_installed,wpb-js-composer js-comp-ver-4.12.1,vc_responsive
 

FizzBuzz in Ruby

FizzBuzz in Ruby

First, some solutions from Github
FizzBuzz Ruby solutions from Github

1:

def fizz_buzz(max)
  arr = []
  (1..max).each do |n|
    if ((n % 3 == 0) && (n % 5 == 0))
      arr << "FizzBuzz"
    elsif (n % 3 == 0)
      arr << "Fizz"
    elsif (n % 5 == 0)
      arr << "Buzz"
    else
      arr << n
    end
  end
  return arr
end

p fizz_buzz(10)
# => [1, 2, "Fizz", 4, "Buzz", "Fizz", 7, 8, "Fizz", "Buzz"]

2:

Nesting in this solution makes it the least appealing.

 def fizz_buzz(max)
  arr = []
  (1..max).each do |n|
    if (n % 3 == 0)
      if (n % 5 == 0)
        arr << "FizzBuzz"
      else
        arr << "Fizz"
      end
    elsif (n % 5 == 0)
      arr << "Buzz"
    else
      arr << n
    end
  end
  return arr
end

p fizz_buzz(10)
# => [1, 2, "Fizz", 4, "Buzz", "Fizz", 7, 8, "Fizz", "Buzz"]

1:

def fizz_buzz(max)
arr = []
(1..max).each do |n|
text = “”
if (n % 3 == 0)
text << "Fizz" end if (n % 5 == 0) text << "Buzz" end if !((n % 3 == 0) || (n % 5 == 0)) text = n end arr << text end return arr end p fizz_buzz(10) # => [1, 2, “Fizz”, 4, “Buzz”, “Fizz”, 7, 8, “Fizz”, “Buzz”]

Tags:
No Comments

Post a Comment