Homework for 3A

Review

Topics Covered: methods, variable scope

Reading

Today's reading is required to complete the exercises. Read Chapters 8 and 14 from Learn to Program. If you're feeling pretty comfortable with the material, feel free to check out Chapters 10 and 11 as an extra bonus.

Exercises

Note: All exercises must live inside of your session0202_exercises/firstname_lastname/d3 folder

While inside of your session0202_exercises directory, you can git add . then git commit -m "Helpful commit message here" and git push origin master to submit your homework to GitHub. (Don't forget to use git status along the way to check the state of your files in git)

E1: Mumble Jumble

Create a file called mumble_jumble.rb. In lecture, we looked at the jumble method (review the slides if you've forgotten). Write a new jumble method that takes a string as an argument, and will return a new string with all the letters in a random order.

jumble("HI THERE I AM STILL HUNGRY")
# => "EUSI LLAM NTGY IR H IHRTHE"

Next, write a mumble method that takes in a string as an argument, and will return the input string in all lower case.

mumble("HOWZIT I AM HUNGRY")
# => "howzit i am hungry"

E2: Scope It Out

Create a file called scope_it_out.md. Look at this code, and answer some questions. As usual, write down your answers first, then try your code in pry. Feel free to discuss your thoughts with others!

loudness = 10
def get_louder
  loudness = 20
  puts "IT'S SO LOUD"
  loudness
end

get_louder
  1. What does get_louder return?
  2. Step by step, describe what the get_louder method does (Hint: there are 3 steps).
  3. What is the value of loudness after the call to get_louder (i.e. at the end of the program)? Why?

Make sure to keep track in your head which variables are inside a method and which variables are outside (indentation really helps to visualize this). No matter what you call your variables, keep track that inside methods, there's a whole new scope—in other words, it's like a whole new program is running.

E3: A Better Bouncer

Yesterday, we created a bouncer program that took in user input from the command line. Today, we'll write a method with arguments for the same functionality.

Create a file called better_bouncer.rb. Write 3 methods: lenient_bouncer, bouncer, and strict_bouncer with the following specifications below:

lenient_bouncer: this dude is essentially asleep on the job and let's everyone in. This method takes in no arguments, and always returns true. Example inputs and outputs below.

lenient_bouncer
# => true

bouncer: he's just doing his job. This method takes in 2 arguments: age (integer) and country (string). This method returns a string, "You in." or "You out." depending on the following laws: 18 and up for South Africa and 21 and up for USA (capitalization shouldn't matter). Example inputs and outputs below.

bouncer(21, 'south africa')
# => "You in."

bouncer(18, 'South Africa')
# => "You in."

bouncer(17, 'usa')
# => "You out."

strict_bouncer: bru's having a bad day. This method takes in 2 arguments. The first argument is an array of people, where a person is represented by the array [name, age]. name is a string and age is an integer.

The second argument is letter, which is a string that is 1 character long.

Since this bouncer is having a bad day, if a person's name begins with the input letter, he or she will also be bounced. This method returns an array of names that are allowed into the 21+ bar. Example inputs and outputs below.

strict_bouncer([['erica', 22], ['ian', 24], ['brian', 34], ['seth', 18]], 'i')
# => ['erica', 'brian']

strict_bouncer([['aaron', 28], ['rafi', 21]], 'i')
# => ['aaron', 'rafi']

Great work! Now add and commit your work then push it to GitHub :)