Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions models/fizzbuzz.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# count to 100
class FizzBuzz

# Counts to 100.
# Makes an array of length 100 where each value is the fizzbuzz value.
def to_array
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you can add the array method after you define the fizz/buzz's

# Start with an empty array.
array = []
# Count from 1 to 100.
(1..100).each do |x|
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try moving this out of the class

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if I do this, should I just get rid of the array?

# If the number matches any of the fizzbuzz numbers, add the string.
if fizzbuzz? (x)
array.push "fizzbuzz"
elsif fizz? (x)
array.push "fizz"
elsif buzz? (x)
array.push "buzz"
# Otherwise just add the number to our array.
else
array.push x
end
end
# Return the finished array of 100 values.
array
end

# FizzBuzz divisible by 3 and 5
# % is modulo division. It divides a number and returns the remainder.
# If the remainder is 0, the number was equally divisible.
def fizzbuzz?(x)
x % 3 == 0 && x % 5 == 0
end

# Fizz divisible by 3
def fizz?(x)
x % 3 == 0
end

# Buzz divisible by 5
def buzz?(x)
x % 5 == 0
end
end
29 changes: 0 additions & 29 deletions models/human.rb

This file was deleted.

61 changes: 61 additions & 0 deletions specs/fizzbuzz_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
require 'rspec'
require_relative "../models/fizzbuzz"


describe FizzBuzz do
describe '.fizzbuzz' do
it 'returns FizzBuzz on numbers divisible by 3 and 5' do
fizzbuzz = FizzBuzz.new
expect(fizzbuzz.fizzbuzz?(15)).to be true
expect(fizzbuzz.fizzbuzz?(16)).to be false
end
end

describe '.fizz' do
it 'returns fizz on numbers divisible by 3' do
fizz = FizzBuzz.new
expect(fizz.fizz?(3)).to be true
expect(fizz.fizz?(4)).to be false
end
end

describe '.buzz' do
it 'returns buzz on numbers divisible by 5' do
buzz = FizzBuzz.new
expect(buzz.buzz?(5)).to be true
expect(buzz.buzz?(74)).to be false
end
end

describe '.to_array' do
it 'makes a fizzbuzz array' do
fizzbuzz = FizzBuzz.new
array = fizzbuzz.to_array
expect(array.count).to eq(100)
# These tests will work because counting of the array starts at 0.
# Eg. 88 is the key, 89 is the value.
expect(array[88]).to eq(89)
expect(array[77]).to eq('fizz')
expect(array[79]).to eq('buzz')
expect(array[74]).to eq('fizzbuzz')
end
end

# Unused play area
# describe 'play area' do
# it 'returns 1 - 100 with fizzbuzz, fizz and buzz' do
# count = FizzBuzz.new
# (1..100).each do |x|
# if count.fizzbuzz (x)
# puts "fizzbuzz"
# elsif count.fizz (x)
# puts "fizz"
# elsif count.buzz (x)
# puts "buzz"
# else
# puts x
# end
# end
# end
# end
end
47 changes: 0 additions & 47 deletions specs/models/human_spec.rb

This file was deleted.