Skip to content

Instantly share code, notes, and snippets.

@nthj
Created December 9, 2013 16:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nthj/7875535 to your computer and use it in GitHub Desktop.
Save nthj/7875535 to your computer and use it in GitHub Desktop.
# 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?
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