diff --git a/README.md b/README.md index f8262fc..cd48169 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,7 @@ More Ruby programming. Create a git branch for ruby-collections-lesson. Then a (1) Write a program which asks for a person's first name, then middle, then last. It should store each of these parts in an array. Finally, it should greet the person using their full name. Call the program full_name.rb. + (2) Write a program called sorted_words.rb. It should prompt the user for words and add each to an array. The user should be able to add as many words as they like, until they just hit enter to return a blank word. Then sort the array using the sort method and print it out. (3) Write a program with a function add_up(i) . It is to be passed a positive integer, and it will add all the numbers from 1 to that integer and return the sum. Call the function three times within the program, and each time print out the return value. Call the program add_up.rb. diff --git a/add_up.rb b/add_up.rb new file mode 100644 index 0000000..0e9dfa3 --- /dev/null +++ b/add_up.rb @@ -0,0 +1,6 @@ +def add_up(a) + sum = 0 + sum = (1..a).inject{ |s, i| s + i } + return sum + end + puts add_up(10) \ No newline at end of file diff --git a/full_name.rb b/full_name.rb new file mode 100644 index 0000000..3bc9994 --- /dev/null +++ b/full_name.rb @@ -0,0 +1,11 @@ +puts "Enter your name" +name = gets.chomp +name = name.to_s +puts "Enter your middle name" +middle_name = gets.chomp +middle_name = middle_name.to_s +puts "Enter your last name" +last_name = gets.chomp +last_name = last_name.to_s +full_name = ['name', 'middle_name', 'last_name'] +puts "Hello " + name + " " + middle_name + " " + last_name + "!" diff --git a/leap_year.rb b/leap_year.rb new file mode 100644 index 0000000..d877bda --- /dev/null +++ b/leap_year.rb @@ -0,0 +1,14 @@ +puts "Enter starting_year" +starting_year = gets.to_i +puts "Enter ending_year" +ending_year = gets.to_i +puts "" +while starting_year.to_i <= ending_year.to_i +if starting_year % 4 == 0 + puts starting_year +elsif starting_year % 100 == 0 +elsif starting_year % 400 == 0 + puts starting_year +end + starting_year = starting_year.to_i + 1 +end \ No newline at end of file diff --git a/sorted_words.rb b/sorted_words.rb new file mode 100644 index 0000000..454876e --- /dev/null +++ b/sorted_words.rb @@ -0,0 +1,8 @@ +puts "Enter as many words as you want, hit enter key twice to exit the game" +word = gets.to_s +words = [] +until (word = gets.chomp).empty? +words << word +puts "Sorted array: #{words.join(" , ")}/" +end +puts "Sorted words: #{words.sort.join(" , ")}." \ No newline at end of file