Skip to content

Latest commit

 

History

History
36 lines (25 loc) · 1.98 KB

File metadata and controls

36 lines (25 loc) · 1.98 KB

Introduction to programming using Ruby: Exercises

  1. Use the modulo operator, division, or a combination of both to take a 4 digit number and find the digit in the: -thousands place -hundreds place -tens place -ones place

  2. Write a program that uses a hash to store a list of movie titles with the year they came out. Then use the puts command to make your program print out the year of each movie to the screen.

  3. Write a program called age.rb that asks a user how old they are and then tells them how old they will be in 10, 20, 30 and 40 years. Below is the output for someone who is 20 years old.

How old are you?
In 10 years you will be:
30
In 20 years you will be:
40
In 30 years you will be:
50
In 40 years you will be:
60```

4. Write a program that prints a greeting message.This program should contain a method called greeting that takes a name as its parameter and returns a string.

5. Write a method that takes a string as argument. The method should return the all-caps version of the string, only if the string is longer than 10 characters. Example: change "hello world" to "HELLO WORLD". (Hint: Ruby's String class has a few methods that would be helpful.)

6. Write a program that takes a number from the user between 0 and 100 and reports back whether the number is between 0 and 50, 51 and 100, or above 100.

7. Write a while loop that takes input from the user, performs an action, and only stops when the user types "STOP". Each loop can get info from the user.

8. Write a method that counts down to zero using recursion.

9. Write a program that iterates over an array and builds a new array that is the result of incrementing each value in the original array by a value of 2. You should have two arrays at the end of this program, The original array and the new array you've created. Print both arrays to the screen using the p method instead of puts.

10. Use the each method of Array to iterate over [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], and print out each value.