Homework for 2A

Review

Topics Covered: Control flow and logic; loops


Exercises

E1: Logic games

Below are a set of logic puzzles. Try to guess the output of each line. To check your answers type (don't copy-paste) the expression into irb.

!true && false
true && false
true && !false
x = 6
x < 7 && x > 5
x <= 6 || x < 5
x < 5 || x > 5
(x = 7) && true # Be careful!
x

E2: Too Old for the Club

Create a new folder for today if you haven't already, called d2. In it, create a new program called strict_bouncer.rb. that gets the age from the console and allows only those in the rage 21 <= age < 65 into the club.

$ ruby strict_bounder.rb
Welcome to da hip hoppin club. What's your age?
20
Too young, fool!
$ ruby strict_bounder.rb
Welcome to da hip hoppin club. What's your age?
65
Go back to the nursing home!
$ ruby strict_bounder.rb
Welcome to da hip hoppin club. What's your age?
64
Aw yeah c'mon in.

E3: Quiz App

In d2, create a mini quiz that prompts for answers and checks if they are right. Your quiz should have at least 3 questions, and keep track of how many the user got right. Use questions about yourself so your friends can learn about you when they take it!

$ ruby mini_quiz.rb
Welcome to Rafi's mini quiz.
What's my last name?
Khan
Right! You have 1 point.
How many siblings do I have?
2
Wrong...I don't have any. You have 1 point.
What country was I born in?
Bangladesh
Nice! You have 2 points.
Thanks for playing!

A user experience concern: how do you know when to display "point" and when to use "points"? Nothing says n00b like a message that reads "You have 1 points". The given answer doesn't address this question, but yours should!

E4: Fizzbuzz

Write a program (also called a script) that prints the numbers from 1-100, but for each multiple of 3, print "fizz", each multiple of 5 print "buzz" and each multiple of 3 and 5 print "fizzbuzz":

$ ruby fizzbuzz.rb
1
2
fizz
4
buzz
...
13
14
fizzbuzz
16
...

The modulo operator returns the remainder when the first input is divided by the second. That is, 10 modulo 4 would return 2. A quick way to check if a number is divisible by another is to see if their modulo is 0. In ruby, the % symbol is the modulo operator, that is 10 % 4 == 2.

E5: The Fibonnaci Code

The Fibonacci sequence is one that starts with 1 and 1, and is made by summing the last two elements of the sequence. Thus, the third element is 1+1=2, the fourth is 1+2=3, the fifth is 2+3=5, and so on. Write a program called fibonnaci.rb that takes an integer from the user and computes that Fibonnaci number. If you've seen recursive Fibonacci before, note, we would like you to write it the iterative way.

$ ruby fibonacci.rb
Enter a number:
1
Fibonnaci at 1: 1
$ ruby fibonacci.rb
Enter a number:
4
Fibonnaci at 4: 3
$ ruby fibonacci.rb
Enter a number:
10
Fibonnaci at 10: 55

Make sure to test your answers against an online Fibonnaci calculator (Wolfram Alpha should do). How long does your code take to compute 10,000? 100,000? 1,000,000? Can you make it faster?


Projects

A Coder's Selfie

Let's continue with our personal pages. Before you arrived in the Mother City, you did some Team Treehouse modules on JavaScript and jQuery. Now, it's time to use this magic world of JS to improve on your personal page. In your personal folder where you're doing your personal page, create a folder called javascript. In javascript, create somefile.js. Then, link this javascript file in your index.html file.

It is possible to write your javascript in your html file itself as shown below. However, as you will write code for your final projects, these javascript files will contain a lot more functionality, so it is important to write JS(javscript) code in a separate file.


  <script>your JS code here</script>
  

So, we know you want to talk a lot about yourself, but I guess you don't want to overwhelm your reader, right? For starters and practice, you will try to 'show' and 'hide' some section of your page by adding a button, which on click, shows some section of your personal page, and on another click, hides it back. Please use Google to your advantage.

And this is how you can include your new somefile.js file in your index.html as shown below:

  <head>
  <title>The Bru</title>
  <script src="somefile.js" type = "text/javascript"></script>
  </head>
  <body>
  ...
  

Push yourself here: you want to have the basic concept done and ready. Look at example sites for inspiration, use Google fonts, borrow other people's styles—you want to have something you're truly proud of! Gauge your progress or get some ideas here.

Code Review

Once again, go through the code review process with someone (different from your partner last time). Check in with a TA to get suggestions.