Created
March 28, 2015 05:05
-
-
Save netsprout/b2b282faae3dc299ccbd to your computer and use it in GitHub Desktop.
Generate HTML Multiplication Table
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
# AngelList Interview | |
# March 27, 2015 | |
# create HTML table given width and height | |
# | |
# Example: | |
# w = 2 | |
# h = 3 | |
# would generate: | |
# - 1 2 | |
# 1 1 2 | |
# 2 2 4 | |
# 3 3 6 | |
def generate_table(width,height) | |
table = "<table>" | |
table += make_header(width) | |
(1..height).each do |y| | |
table += " \n<tr>\n" | |
table += " <td>#{y}</td>" | |
(1..width).each do |x| | |
table += " <td>#{x*y}</td>" | |
end | |
table += " \n</tr>\n" | |
end | |
table += "</table>\n" | |
end | |
def make_header(width) | |
puts "W: #{width}" | |
header = "\n<tr>\n" | |
(0..width).each do |w| | |
header += "<th>#{w}</th>" | |
end | |
header += "\n</tr>\n" | |
end | |
# Run program | |
puts generate_table(2,3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Generate Multiplication Table