-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDay3JSLE.rb
More file actions
107 lines (73 loc) · 2.77 KB
/
Day3JSLE.rb
File metadata and controls
107 lines (73 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#Student Hala Haddad
#this document includes the solutions to day 3 jumpstart live exercieses
#1.Write a program that allows a user to play a guessing number game.
#Your program should generate a random number between 0 – 1000
#(including 0, but not including 1000).
#Allow the user to make a guess until they guess the answer.
#After each guess you should print "higher" or "lower".
#When they guess it correctly print a winning message along with their total number of guesses.
puts "Guess my number: "
n = gets.chomp.to_i #converting user input to integer
r = rand(1000) #picks a random number bigger than 0 and less than (but does not include) 1000
g = 1 #guess counter
while n != r #loop keeps going as long as the players guess does not equal the random number picked by the program
if r > n
puts "higher"
puts "guess again:"
n = gets.chomp.to_i
g += 1
elsif r < n
puts "lower"
puts "guess again:"
n = gets.chomp.to_i
g +=1
end
end
puts "YOU GOT IT IN #{g} GUESSES!"
#2. Write a program that plays duck duck goose. Allow the user to enter
#the player's number they want to call goose on, and then say "duck"
#for each player before the "goose", then say "goose" for the chosen player.
puts "Let's play Duck Duck Goose!"
puts "Which player do you want to Goose? "
n = gets.chomp.to_i
c = 1 #keeps tally of number of times duck is printed to the console, adds one for goose to reach total number
while n != c
puts "Player #{c}: Duck"
c +=1
end
puts "Player #{c}: Goose"
#Write a program that allows a user to enter the number of petals on a flower.
#Then one by one, print “plucking petal #1: they love me!”.
#Alternate “They love me” and “They love me not” as well as increase the petal number for each petal.
puts "enter the number of petals on a flower: "
p = gets.chomp.to_i
c = 1 #keeps count of petal numbers
while c != p+1
if c%2 == 1 #when petals numbers is odd, first petal is always odd
puts "Plucking petal #{c}: They love me!"
c+=1
else #when petal numbers is even
puts "Plucking petal #{c}: They love me not!"
c+=1
end
end
#You don't trust your users.
#Modify the program below to require the user to
#enter the same value twice in order to add that value to the total.
puts "Hello! We are going to total some numbers!"
puts "Enter a negative number to quit."
total = 0
input1 = gets.chomp.to_i #prompting user twice
input2 = gets.chomp.to_i
while input1 > -1 #this while loop makes sure the user in entering equal values for input1 and input2
if input1==input2
total += input1
input1 = gets.chomp.to_i
input2 = gets.chomp.to_i
else
puts "inputs do not match! try again" #if input1 and input2 do not match
input1 = gets.chomp.to_i
input2 = gets.chomp.to_i
end
end
puts "Result: #{total}"