This is the first of a series of short posts in which I solve interview questions from my Professional Practices course using Ruby.

The Prompt

Write a program using for loops (in Java, JavaScript, or C) that prints out the numbers 1 through 10 on the first line, the squares of 1 through 10 on the second line (e.g. 1, 4, 9…), the cubes on line three (e.g. 1, 8, 27…), and so on, for ten lines. Please be sure to indicate which language you are using.

Hint: How do I write a program that displays numbers 1 to 10 and their squares?

The Solution

(1..10).each do |exponent|
  array = []
  (1..10).each { |baseNumber| array.push(baseNumber ** exponent) }
  puts array.join(", ")
end