Search⌘ K

Solution: Printing a Half Diamond

Explore how to print a half diamond pattern in Ruby by using nested loops and conditional checks. Understand the use of loop control variables and how to handle zero-based indexing to create symmetrical shapes. This lesson helps you practice managing loops and outputs for shape printing tasks.

Courtney’s version

Ruby 3.1.2
count = 0
8.times do
count += 1 # this is equivalent to count = count + 1
hashes = "#" * count
puts hashes
end
count = 8
8.times do
count -= 1
hashes = "#" * count
puts hashes
end

Explanation

Courtney’s version code loops 16 times, (two loops of 8 iterations each) , but prints out 15 lines of the half diamond. This is because in ...