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?
  9. What special feature do named functions have in Javascript?
  10. Why do we prefer anonymous functions over named functions in modern Javascript?
  11. Which built-in array function takes a list of elements and turns them into another list of elements by using a callback function over each element?
  12. Which type of function do we use to create an instance of a class with some default values?
  13. If we want to inherit the properties of a parent class in a child class, what do we need to do?
  14. What's a common use of finally in error handling?

Exercises

  1. Write a while loop that iterates backwards through an array of numbers, logging out each element to the console.
  2. Write a function that takes in an array of strings and returns the shortest string in the array.
  3. 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".
  4. 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}].
  5. Write a function to count from a number n to zero without using a loop.
  6. Write a function or class which creates 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.