Last active
December 20, 2015 18:19
-
-
Save TravisL12/6175381 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| # Andrews' CODE | |
| def times_table(rows) # <-- defines method and how many rows passed as argument | |
| 1.upto(rows) do |x| # <-- You start first loop with value x | |
| line = "" | |
| 1.upto(rows) do |y| # <-- begin 2nd (nested) loop with value y | |
| line += "#{x*y}\t" # <-- x=1 therefore 1*1, 1*2, 1*3. When done x=2 2*1, 2*2, 2*3.... (x=3, 3*1, 3*2, 3*3...) | |
| end | |
| puts line | |
| end # <-- incremement to the next x value | |
| end | |
| # The basics are, that you will increment an 'x' value and then run the entire 'y' loop. | |
| # Then you increase 'x' and then run the whole 'y' loop again. | |
| # An alternative looping system could be written without the 'line' variable: | |
| 1.upto(rows) do |x| | |
| 1.upto(rows) do |y| | |
| print "#{ x * y }\t" | |
| end | |
| puts | |
| end | |
| # Take note of why the 'puts' is placed after the 'y' loop. | |
| # That's to give a line break after each row of values has been printed. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Interesting. Your way is definitely better. Thanks for clearing that up. My line spacing was redundant with the \t.