Homework for 2B

Review

Topics Covered: Data Types, Booleans, Control flow and logic


Exercises

E1: Logic games

Below are a set of logic puzzles. Try to guess the output of each line. To check your answers type (don't copy-paste) the expression into pry.

!true && false
true && false
true && !false
x = 6
x < 7 && x > 5
x <= 6 || x < 5
x < 5 || x > 5
(x = 7) && true # Be careful!
x

E2: Too Old for the Club

Create a new folder for today if you haven't already, called d2. In it, create a new program called strict_bouncer.rb. that gets the age from the console and allows only those in the rage 21 <= age < 65 into the club.

$ ruby strict_bounder.rb
Welcome to da hip hoppin club. What's your age?
20
Too young, fool!
$ ruby strict_bounder.rb
Welcome to da hip hoppin club. What's your age?
65
Go back to the nursing home!
$ ruby strict_bounder.rb
Welcome to da hip hoppin club. What's your age?
64
Aw yeah c'mon in.

E3: Quiz App

In d2, create a mini quiz that prompts for answers and checks if they are right. Your quiz should have at least 3 questions, and keep track of how many the user got right. Use questions about yourself so your friends can learn about you when they take it!

$ ruby mini_quiz.rb
Welcome to Rafi's mini quiz.
What's my last name?
Khan
Right! You have 1 point.
How many siblings do I have?
2
Wrong...I don't have any. You have 1 point.
What country was I born in?
Bangladesh
Nice! You have 2 points.
Thanks for playing!

A user experience concern: how do you know when to display "point" and when to use "points"? Nothing says n00b like a message that reads "You have 1 points". The given answer doesn't address this question, but yours should!