JavaScript Review I

So far we've covered some of the basic topics in programming:

  • Variables and data types
  • Operators of different types
  • Control flow with loops and if statements
  • Working with strings, arrays, and objects
  • Using functions in JavaScript

We'll quickly go through some review questions today to try and refresh your knowledge in each of these areas.

Questions

  1. What is the difference between =, == and === in JavaScript?
  2. How do you add a number to a variable using only one operator in JavaScript?
  3. What happens if you add 1 and the string "10" in Javascript?
  4. What's the main usage of a for loop? What's the main usage of a while loop?
  5. If I want to get the 5th character in the string "My name is Frank." how can I do this?
  6. Write an if statement to check if a variable i is divisible by 3. Write a message indicating if it is or isn't.
  7. What is the difference between an array and an object in JavaScript? Give an example of each.
  8. Why would you use an object to store a list of names and email addresses, rather than an array?

Exercises

  1. Write a for loop that iterates over an array of numbers and logs each number to the console.
  2. Write a while loop that iterates backwards through an array.
  3. Write a function that takes in an array of strings and returns the shortest string in the array.
  4. Write a function that takes in a string as a parameter and returns a new string with the first letter of each word capitalized. For example, if the input is "hello world", the output should be "Hello World".
  5. Write a function that takes in an array of objects and an object key, and returns a new array with only the objects that have a property matching the key. For example, if the input is [{name: "John", age: 30}, {name: "Mary", age: 25}, {name: "Bob", age: 30}], and the key is {age: 30}, the output should be [{name: "John", age: 30}, {name: "Bob", age: 30}].
  6. Write a function which returns a button object. This object should have a property called onTap which can be set after you return the object. It should have another property called Tap which is a function which calls OnTap. Finally, create a button, set OnTap to a function which calls console.log(), and tap the button.