Last active
August 29, 2015 14:18
-
-
Save PamBWillenz/184485081a975cfca354 to your computer and use it in GitHub Desktop.
If Statements- Bloc Checkpoint
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Specs | |
LOCK METHOD | |
The lock method should take four arguments: | |
def lock(a,b,c,d) | |
end | |
Each argument represents a number on a combination lock. The method will return "unlocked" or "locked", depending on whether the combination is correct. There are several combinations that are "correct". | |
The first number has to be a 3, 5, or 7 | |
The second number has to be a 2 | |
The third number has to be a 5 or a 6 | |
The fourth number has to be 8, 9 or 0 | |
We could test the first condition like so: | |
if (a == 3 || a == 5 || a == 7) | |
describe "lock" do | |
it "should return unlocked for 3258" do | |
expect( lock(3, 2, 5, 8) ).to eq('unlocked') | |
end | |
it "should return locked for 1111" do | |
expect( lock(1, 1, 1, 1) ).to eq('locked') | |
end | |
it "should return locked for 3111" do | |
expect( lock(3, 1, 1, 1) ).to eq('locked') | |
end | |
it "should return unlocked for other valid combinations" do | |
expect( lock(3, 2, 5, 8) ).to eq('unlocked') | |
expect( lock(5, 2, 5, 0) ).to eq('unlocked') | |
expect( lock(5, 2, 6, 8) ).to eq('unlocked') | |
expect( lock(7, 2, 5, 8) ).to eq('unlocked') | |
expect( lock(7, 2, 6, 9) ).to eq('unlocked') | |
end | |
end | |
CORRECT CODE: | |
def lock(a, b, c, d) | |
if (a == 3 || a == 5 || a == 7) | |
"unlocked" | |
end | |
if (b == 2) | |
"unlocked" | |
end | |
if (c == 5 || c == 6) | |
"unlocked" | |
end | |
if (d == 8 || d == 9 || d == 0) | |
"unlocked" | |
else | |
"locked" | |
end | |
end | |
lock(3, 2, 5, 8) | |
lock(1, 1, 1, 1) | |
lock(3, 1, 1, 1) | |
lock(5, 2, 5, 0) | |
lock(5, 2, 6, 8) | |
lock(7, 2, 5, 8) | |
RESULTS | |
lock should return unlocked for 3258 | |
lock should return locked for 1111 | |
lock should return locked for 3111 | |
lock should return unlocked for other valid combinations | |
Can-I-Get? Method | |
The can_i_get? method should take two arguments: | |
def can_i_get?(item,money) | |
end | |
The first argument is a string representing what the user wants to buy. | |
According to the specs below, the user can buy a computer or iPad. | |
The second argument is a number representing how much money the purchaser has. The method should evaluate if she has enough money to buy a computer or iPad -- returning true if so, and false if not. An example for the first condition could be: | |
if item == "computer" && money >= 1000 | |
Try to use elsif rather than separate if statements. | |
describe "can_i_get?" do | |
it "returns true if user wants a computer and has $1,000" do | |
expect( can_i_get?("computer", 1100) ).to eq(true) | |
end | |
it "returns false for a computer if they don't have $1,000" do | |
expect( can_i_get?("computer", 900) ).to eq(false) | |
end | |
it "returns true for a iPad if they have $500" do | |
expect( can_i_get?("iPad", 500) ).to eq(true) | |
end | |
it "returns false for a iPad if they have less than $500" do | |
expect( can_i_get?("iPad", 499) ).to eq(false) | |
end | |
end | |
CORRECT CODE: | |
def can_i_get?(item, money) | |
if item == "computer" && money >= 1000 | |
true | |
elsif item == "computer" && money < 1000 | |
false | |
elsif item == "iPad" && money >= 500 | |
true | |
elsif item == "iPad" && money < 500 | |
false | |
end | |
end | |
can_i_get?("computer", 1100) | |
can_i_get?("computer", 900) | |
can_i_get?("iPad", 500) | |
can_i_get?("iPad", 499) | |
RESULTS | |
can_i_get? returns true if user wants a computer and has $1,000 | |
can_i_get? returns false for a computer if they don't have $1,000 | |
can_i_get? returns true for a iPad if they have $500 | |
can_i_get? returns false for a iPad if they have less than $500 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Specs | |
describe "favorite_number" do | |
it "should return 'Too low' if the guess is low" do | |
expect( favorite_number(10, 1) ).to eq("Too low") | |
end | |
it "should return 'Too high' if the guess is high" do | |
expect( favorite_number(5, 11) ).to eq("Too high") | |
end | |
it "should return 'You got it!' if the guess is right" do | |
expect( favorite_number(11, 11) ).to eq("You got it!") | |
end | |
end | |
def favorite_number(fav, guess) | |
end | |
The favorite_number method should: | |
return "Too high" if guess is greater than fav | |
return "Too low" if guess is less than fav | |
return "You got it!" if guess is equal to fav | |
IF STATEMENT - correct code | |
def favorite_number(fav, guess) | |
if guess < fav | |
"Too low" | |
elsif guess > fav | |
"Too high" | |
elsif guess == fav | |
"You got it!" | |
end | |
end | |
favorite_number(10, 1) | |
favorite_number(5, 11) | |
favorite_number(11, 11) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment