diff --git a/models/fizzbuzz.rb b/models/fizzbuzz.rb new file mode 100644 index 0000000..6389fec --- /dev/null +++ b/models/fizzbuzz.rb @@ -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 + # Start with an empty array. + array = [] + # Count from 1 to 100. + (1..100).each do |x| + # 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 diff --git a/models/human.rb b/models/human.rb deleted file mode 100644 index 92c6c1a..0000000 --- a/models/human.rb +++ /dev/null @@ -1,29 +0,0 @@ -class Human - - def initialize value - @name = value - end - - def name - @name - end - - def legal_name - first_name = @name.split.first - last_name = @name.split.last - - last_name + ", " + first_name - - # @name.split(' ', 2).reverse.join(', ') - end - - # attr_accessor :job_title - def job_title=(value) - @job_title = value - end - - def job_title - @job_title - end -end - diff --git a/specs/fizzbuzz_spec.rb b/specs/fizzbuzz_spec.rb new file mode 100644 index 0000000..747fd93 --- /dev/null +++ b/specs/fizzbuzz_spec.rb @@ -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 diff --git a/specs/models/human_spec.rb b/specs/models/human_spec.rb deleted file mode 100644 index e0f705d..0000000 --- a/specs/models/human_spec.rb +++ /dev/null @@ -1,47 +0,0 @@ -require 'rspec' -require_relative "../../models/human" - -describe Human do - - describe '#new' do - it 'initializes a new instance of Human' do - human = Human.new('Fred') - expect(human).to be_instance_of Human - end - end - - describe '.name' do - it 'has a name of Fred' do - human = Human.new('Fred') - expect(human.name).to eq('Fred') - end - end - - describe '.legal_name' do - it 'that "Fred Frog" returns "Frog, Fred"' do - human = Human.new('Fred Frog') - expect(human.legal_name).to eq('Frog, Fred') - end - end - - describe '.job_title=' do - it 'sets the job title for our human instance' do - human = Human.new('') - expect(human).to respond_to(:job_title=) - end - end - - describe '.job_title' do - it 'it returns the job title' do - human = Human.new('') - human.job_title = 'Guru' - expect(human.job_title).to eq('Guru') - end - end - - it 'play area' do - puts Human.new('Caitlin').name - end -end - -