Last active
August 29, 2015 14:20
Revisions
-
timkellogg revised this gist
May 9, 2015 . 1 changed file with 41 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,41 @@ # Making loops in many ways list = [1,2,3,4,5,6,7,8,9,10] # each loop sum = 0 list.each { |num| sum+=num } puts "Each: #{sum}" # while loop sum = 0 index = 0 while list.length > index value = list[index] sum += value index += 1 end puts "While: #{sum}" # for loop sum = 0 for i in list do sum += i end puts "For: #{sum}" # loop sum = 0 list.length.times { |num| sum += num } puts "Loop: #{sum}" # until sum = 0 index = 0 until index = list.length value = list[index] sum += value index += 1 end puts "Until: #{until}" -
timkellogg renamed this gist
May 9, 2015 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
timkellogg created this gist
May 9, 2015 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,14 @@ puts "Enter a number you want to calculate up to in the Fibonacci sequence" max = gets.chomp #Fibonacci sequence = [] 0.upto(max.to_i) do |num| if num > 2 what_to_push = sequence[num - 1] + sequence[num - 2] sequence << what_to_push else sequence << num end end puts "#{sequence}" 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,15 @@ Fizzbuzz game puts "Please enter a number you want to fizzbuzz up to" user_input = gets.chomp 1.upto(user_input.to_i) do |num| if num % 15 == 0 puts "FizzBuzz" elsif num % 3 == 0 puts "Fizz" elsif num % 5 == 0 puts "Buzz" else puts num end end