-
Notifications
You must be signed in to change notification settings - Fork 0
/
loopy_birds.rb
76 lines (58 loc) · 1.92 KB
/
loopy_birds.rb
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
# All about loops in Ruby
# For loop - not really used in Ruby!
# for n in 1..100
# puts "#{n} birds on a wire - AH AH AH"
# end
# While and until loops - used when it's unclear how many times we need to loop
# understands_loops = "no"
# # while understands_loops != "yes"
# # puts "Study more! \n... do you understand them now?"
# # understands_loops = gets.chomp
# # end
# until understands_loops == "yes"
# puts "Study more! \n... do you understand them now?"
# understands_loops = gets.chomp
# end
# Times do loop - definitely more Ruby-ish
# # |i| is the block variable, in this case it starts at 0 and increments by 1
# 5.times do |i|
# puts "#{i + 1} birds on a wire - AH AH AH"
# end
# 3.times do
# puts "She loves you, yeah yeah yeah!"
# end
# Write a loop that prints out the even numbers from 1 to 100.
# current_number = 2
# while current_number <= 100
# puts current_number
# # This is the same as current_number = current_number + 2.
# current_number += 2
# end
# 50.times do |i|
# puts (i + 1) * 2
# end
# for n in 1..100
# puts n if n % 2 == 0
# end
# Inline loops - curly brackets replace do and end
# 50.times { |i| puts (i + 1) * 2 }
# 1.upto(100){ |popcorn| puts popcorn if popcorn % 2 == 0 }
# (1..10).each do |popcorn|
# puts "#{popcorn} kernels of yummy"
# end
# Classroom Challenge (pair program): Create an array with at least 5 elements. Call the .reverse and .length methods on your array. Try out nesting the .shuffle and .join methods to list your array objects in random order, separated by commas.
# colors = ["red", "blue", "green", "lavender", "periwinkle", "fuchsia"]
# puts "#{colors.reverse}"
# puts colors.length
# puts colors.shuffle.join(",")
# puts "#{colors}"
# Each loops with arrays
scores = [100, 80, 75, 93]
sum = 0
# scores.each do |score|
# # same as sum = sum + score
# sum += score
# end
# inline
scores.each { |score| sum += score }
puts sum / scores.length