Created
December 9, 2013 21:05
-
-
Save nthj/7880871 to your computer and use it in GitHub Desktop.
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
# Quiz Exercise, Part One | |
# | |
# Quiz for a name and birthday, | |
# then display the age | |
# | |
# For example: | |
# | |
# Hey there! Please enter your name: | |
# > John | |
# | |
# Thanks. When is your birthday, John? | |
# > 10/13/2000 | |
# | |
# You are 13 years old. | |
## | |
## OUTLINE | |
## | |
# We're going to want to break the problem down into steps. | |
# First, let's ask for the user's input: | |
# | |
puts "Hey there! Please enter your name:" | |
# How do we prompt for input from the keyboard in Ruby? | |
# HINT: this will cause weird behavior. DON'T DO THIS: | |
# gets "name" | |
# | |
# Instead, just use `gets`. This returns whatever value the user types in | |
# gets | |
name = 'John' | |
# BIRTHDAY | |
# OK, now birthday | |
puts "Thanks. When is your birthday, John?" | |
birthday = '10/13/2000' # TODO: ask for the user to type in a birthday | |
# AGE | |
# We have to calculate this based on their birthday | |
# | |
# Tip: solve a couple real-life problems in your head: | |
# If I told you I was born in 1976, how would you calculate how old I am? | |
# | |
# Hints: | |
# * How do we extract just the age from a string like '10/13/2000'? | |
# * How would we perform arithmetic on the string '2000'? | |
# * You can just hard-code the current year, "2013", into your script, and replace it later with the computer's clock time | |
age = 13 # TODO: calculate the age | |
puts "You are 13 years old." # TODO: display calculated age | |
## | |
## FINISHING UP | |
## | |
# * Does your script ask for user input? Will it run with examples | |
# other than "John" and "10/13/2000?" | |
# | |
# | |
# * Are there line breaks any place you don't want them? e.g.: | |
# | |
# You are 13 | |
# years old | |
# | |
# ? How do we remove line breaks in Ruby? | |
# | |
# | |
# * Is the age calculation correct when the birthday is after the current date? | |
# What happens if you enter the birthday "12/29/1950" | |
## | |
## EXTENSION #1 | |
## | |
# Extensions are optional. | |
# Don't worry if you don't have time to complete them. | |
# Display a checklist of things they're able to do: | |
# | |
# * Are they eligible for a driver's license? | |
# * Are they allowed to join the military? | |
# * Are they allowed to buy alcohol? | |
# * Are they eligible for a senior citizen discount? | |
# | |
# Example output: | |
# | |
# Driver's license: YES | |
# Military: YES | |
# Alcohol: NO | |
# Senior citizen: NO | |
## | |
## EXTENSION #2 | |
## | |
# How would we use the Ruby Time library to perform the arithmetic using Time objects? | |
# | |
# Hint: you'll need to add this line of code to the top of your script | |
# to get access to all of Ruby's time functionality: | |
# | |
# require 'time' | |
# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment