Instructions
Create a function even? that returns true if a number is even, false otherwise.
Hint: Use the modulo operator % to check divisibility.
Hints:
- A number is even if n % 2 == 0
- Ruby has a built-in .even? method on integers
- You can use either approach
Your Code
def even?(n) n % 2 == 0 end
RSpec.describe "even?" do
it "returns true for 0" do
expect(even?(0)).to be true
end
it "returns true for 2" do
expect(even?(2)).to be true
end
it "returns false for 1" do
expect(even?(1)).to be false
end
it "returns true for negative even" do
expect(even?(-4)).to be true
end
it "returns false for negative odd" do
expect(even?(-3)).to be false
end
end
Results
Click "Run Tests" to see results
1 / 3
Next: FizzBuzz