How to resolve the algorithm FizzBuzz step by step in the Ruby with RSpec programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm FizzBuzz step by step in the Ruby with RSpec programming language
Table of Contents
Problem Statement
Write a program that prints the integers from 1 to 100 (inclusive).
But:
The FizzBuzz problem was presented as the lowest level of comprehension required to illustrate adequacy.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm FizzBuzz step by step in the Ruby with RSpec programming language
Source code in the ruby programming language
require 'fizzbuzz'
describe 'FizzBuzz' do
context 'knows that a number is divisible by' do
it '3' do
expect(is_divisible_by_three?(3)).to be_true
end
it '5' do
expect(is_divisible_by_five?(5)).to be_true
end
it '15' do
expect(is_divisible_by_fifteen?(15)).to be_true
end
end
context 'knows that a number is not divisible by' do
it '3' do
expect(is_divisible_by_three?(1)).not_to be_true
end
it '5' do
expect(is_divisible_by_five?(1)).not_to be_true
end
it '15' do
expect(is_divisible_by_fifteen?(1)).not_to be_true
end
end
context 'while playing the game it returns' do
it 'the number' do
expect(fizzbuzz(1)).to eq 1
end
it 'Fizz' do
expect(fizzbuzz(3)).to eq 'Fizz'
end
it 'Buzz' do
expect(fizzbuzz(5)).to eq 'Buzz'
end
it 'FizzBuzz' do
expect(fizzbuzz(15)).to eq 'FizzBuzz'
end
end
end
def fizzbuzz(number)
return 'FizzBuzz' if is_divisible_by_fifteen?(number)
return 'Buzz' if is_divisible_by_five?(number)
return 'Fizz' if is_divisible_by_three?(number)
number
end
def is_divisible_by_three?(number)
is_divisible_by(number, 3)
end
def is_divisible_by_five?(number)
is_divisible_by(number, 5)
end
def is_divisible_by_fifteen?(number)
is_divisible_by(number, 15)
end
def is_divisible_by(number, divisor)
number % divisor == 0
end
You may also check:How to resolve the algorithm Sum of squares step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Integer sequence step by step in the Ruby programming language
You may also check:How to resolve the algorithm Zero to the zero power step by step in the Pyret programming language
You may also check:How to resolve the algorithm Active Directory/Connect step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Loops/Downward for step by step in the GML programming language