panicbus
12/6/2013 - 7:06 PM

test.md

Test

Ruby

  1. Print the text "Hello, world!"

    puts "Hello, World"

  2. What data type did you print?

     string
    
  3. Print the result of the expression 2 + 2

     p 2+2
    
  4. What data type did you print?

     integer
    
  5. Assign the result of the expression 2 + 2 to a variable named result

     result = 2 + 2
    
  6. What data type is the value of result?

     integer
    
  7. Write a method which takes two parameters and adds them, returning the result.

     def addTwo(a,b)
         p a + b
     end
    
  8. On what datatypes will the function you wrote work correctly? Incorrectly?

     It will work correctly with strings, but with integers it will only concatenate the numbers, not add them.
    
  9. What is the difference between testing for equality and assigning a value to a variable? (Show an example of both)

     a == 3 tests for equality
     a = 3 assigns value to a
    
  10. What data type is [1, 2, 3, 4, 5]?

     an array
    
  11. Iterate through the above data structure, printing each number multiplied by two.

     arr = [1, 2, 3, 4, 5]
     for arr.each do |a|
         p a * 2
     end 
    
  12. Iterate through the above data structure, printing "I am the [n]th element", replacing 'n' with each element's index.

     arr = [1, 2, 3, 4, 5]
     for arr.each do |i|
         p "I am the #{arr[i]}th element."
     end 
    
  13. What data type is {cats: 3, dogs: 2, fish: 13}?

     a hash
    
  14. What is the data type and role of cats in the above structure?

     a key of a key/value pair
    
  15. What are the roles of the integers in the above structure?

     they're values
    
  16. Iterate through the above data structure, printing "Kyle has [n] [thing]s", replacing 'n' and 'thing' with the appropriate values.

  1. Write a method called max_animal that accepts a data structure like the above as a parameter, and returns which type of animal Kyle has the most of.
  1. Write a class called Man with an instance method move, which prints walk, and an attribute name with a reader and writer method.

     class Man
     
     attr_accessible :name
    
     def initialize(move)
         @move = move
         @name = name
     end
    
     def walk
         p name.walk
     end
    
     end
     name = Man.new
    
  2. Write a subclass of Man called Superman, whose move method prints fly

     class Superman < Man
     
     def move
         
     end
     
     end
    
  3. Add a class method to your Superman class called is_superhero? which returns true.

  1. Do you need to add the name attribute to Superman? If not, why?
  1. Does Ruby have methods, functions, or both?

     ruby has methods
    
  2. Whichever of the above (method/function) you decided, can it be stored in a variable?

     yes
    
  3. What is the name of this construct in Ruby: do |thing| thing * 2 end

     loop
    

Javascript

  1. Print the text "Hello, world!"

     console.log('Hello, world!');
    
  2. Print the result of the expression 2 + 2

     console.log(2 + 2)
    
  3. Assign the result of the expression 2 + 2 to a local variable named result

     result = 2 + 2
    
  4. Write a function which takes two parameters and adds them, returning the result.

     var addTwo = function(a,b){
         a + b;
     }
     addTwo(1,2);        
    
  5. On what datatypes will the function you wrote work correctly? Incorrectly?

     integers
    
  6. What data type is [1, 2, 3, 4, 5]?

     an array
    
  7. Iterate through the above data structure, printing each number multiplied by two.

     arr = [1, 2, 3, 4, 5]
     for (var = i; i > arr.length; i++){
         console.log(arr[i] * 2);
         }   
    
  8. What data type is {cats: 3, dogs: 2, fish: 13}? (Hint: NOT the same as the Ruby answer)

  1. In setTimeout(doStuff, 2000), what datatype should doStuff be and what do we call that datatype in this role?

     do stuff is an object and in this role it's something else 
    
  2. What datatype is foo in var foo = function() { alert("Hello"); };?

     object
    
  3. Is a function a type of object?

     yes
    
  4. Show how you would use var Person = function(){}; to construct a Person.

  1. What is a prototype?

     a prototype is an object that stands in for something more complicated declared earlier in the program
    
  2. What do I mean every time I say "In Javascript, functions are first-class values"?

     they're top level, 
    
  3. What is the difference between an AJAX request and a typical HTTP request?

    ajax requests happen on the same page, without having to refresh

Rails

  1. What type (three letter acronym) of framework is Rails?

     MVC
    
  2. Define each of the three components

     M = Model: where the data is gathered and stored
     V = View: where Rails interacts with the browser
     C = Controller: handles the transferring and placement of data
    

In your shell...

  1. Make a new rails project called blog

     rails new blog
    
  2. In that project, make a new controller PostsController

     rails g controller posts
    
  3. Make a Post model

     rails g model post
    
  4. Make PostsController and the Post model, as well as RESTful routes, in a single step.

     rails g controller posts model posts index
    
  5. Run the migrations for Post

     rake db:migrate
    
  6. See all your app's routes

     rake routes
    
  7. Start the web server

     rails s
    

In general...

  1. What is the name of the (default) superclass of all of our generated controllers?

     Base
    
  2. What do we call controller instance methods that respond to web requests?

     ajax
    
  3. What, by default, would one such method (say, show) do if I leave its method body empty?

     display the result of a show request
    
  4. What kind of template is application.html.erb?

     view
    
  5. What kind of template is posts/_form.html.erb?

     form partial
    
  6. In an ERB template, with a @post var available, how would I render a link to that post's show page?

     <%= link_to "That one", @post_path %>
    
  7. What relationships would you define in your models if a user can have multiple posts?

     blog :has_many posts
    
  8. What field would you need to add to post?

     post :belongs_to blog
    
  9. What relationships would you define in your models if a post could have multiple categories, and a category could belong to multiple posts? What tables would you need to add?

     join table
    

In the Rails console...

  1. How many Posts exist in the database?

     Post.all
    
  2. How many Posts by user with id 1? (assuming I've properly associated a User model with the Post model)

     Post.all(1)
    
  3. Find the first post

     Post.first
    
  4. Destroy the last post

     Post.delete.last
    
  5. Destroy all posts by user with id 5.

     Post.delete.where('userId' = 1)
    
  6. Create a post with user id 1, title "My Post", and body "This is the body of my first post"

  1. Find all the categories associated with the first post.
  1. Print out all the titles of the categories associated with the first post.

jQuery

  1. Select an element with id foo

     $('#foo').select
    
  2. Select all elements with class bar

     $('.bar').select.all
    
  3. Select all the <div>s on the page.

     $('div').select
    
  4. Create a new div with id baz.

     $('div').create('#baz')
    
  5. Append that newly created div to a div with id foo

     $('#baz).appendTo('#foo')
    
  6. Remove all input elements from the page.

     $('input').delete
    
  7. Remove all the children of the div with id foo

     $('#foo').children.delete
    
  8. Hide all divs with class oops

     $('.oops').hide
    
  9. Hide all the children of the div with id foo

     $('.foo').children.hide
    
  10. Perform an AJAX GET request to /thingList.json, iterating through the returned result set and logging each item to the console.

     i'd have to look at my notes to get this (of which i have many FTR)
    

Misc

  1. Explain how APIs work. In detail.

    • Definition

      an API is a protocol to gather information from a third party website

    • How you interact with them

      with ajax requests

    • Why/when you need to use them

      when you want to use data that someone else has compiled

    • When you might build one yourself

      If a data source does not currently have a properly functioning API or one at all.

  2. What is the purpose of yield? (Ruby) Show an example.

     yield brings in info from other pages in your project 
    
    • What Javascript concept is the analogous to?
  1. Construct a regular expression that matches a valid email address.
  1. Explain how http works in detail.

     It sends a message to the server to look for a given URL, then gathers that data back to the server and displays the HTML files in the browser  
    
  2. Write a method that appends the words “in Ruby” to any English sentence, in Ruby.

     puts "Type a sentence:"
     sent = gets.chomp
     puts sent + " in Ruby."
    
  3. Write a method that converts x amount of dollars into change and specify how much of each coin will you have. E.g. convert $2.65 and return the amount of quarters, dimes, nickels, and pennies.

* Use a regular expression to extract the dollars and cents from the string


* You may use <http://rubular.com>
  1. What's the difference between ActiveRecord callbacks and validations?
  1. In Javascript, what's the difference between null and undefined?

     Null mean that there is nothing there, that there is nothing to equate to, undefined means that you haven't yet set the equation's value. 
    
  2. When would you use a CSS float?

     When you want elements to line up horizontally.
    
  3. What is the "Box Model" in CSS? Which CSS properties are a part of it?

     A box model are the elements that surround every CSS element. The consist of the element, padding, border and margin.