Created
February 18, 2020 23:05
-
-
Save marcoranieri/a7eaed8081a5370651e0b6905099a30e to your computer and use it in GitHub Desktop.
Programming Basics
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
# TODO Guess the price! | |
# Generate a random number (computer choice) | |
computer = rand(1..5) | |
loop do | |
# prompt the user to guess a number | |
puts "Guess a number!" | |
# from String (gets.chomp) to Integer (.to_i) | |
user = gets.chomp.to_i | |
# compare computer choice with user guess | |
if computer == user | |
puts "YOU WIN!" | |
break # EXIT the loop | |
else | |
puts "YOU LOSE!" | |
end | |
end |
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
require 'date' | |
# TODO define a method to display number of days until xmas | |
def days_until_xmas(year=Date.today.year) | |
# define xmas (Date class) | |
xmas = Date.new(year,12,25) | |
# define today (Date class) | |
today = Date.today | |
if xmas < today | |
xmas = Date.new(year + 1,12,25) | |
end | |
# subtract xmas - today | |
diff_days = xmas - today | |
#return correct data type (Integer) | |
return diff_days.to_i | |
end | |
# TDD - test driven development | |
puts days_until_xmas | |
puts days_until_xmas.class == Integer | |
# puts days_until_xmas == 311 # 18/02/2020 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment