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
- What is the difference between =, == and === in JavaScript?
- How do you add a number to a variable using only one operator in JavaScript?
- What happens if you add 1 and the string
"10"
in Javascript? - What's the main usage of a for loop? What's the main usage of a while loop?
- If I want to get the 5th character in the string
"My name is Frank."
how can I do this? - Write an if statement to check if a variable
i
is divisible by 3. Write a message indicating if it is or isn't. - What is the difference between an array and an object in JavaScript? Give an example of each.
- Why would you use an object to store a list of names and email addresses, rather than an array?
- What special feature do named functions have in Javascript?
- Why do we prefer anonymous functions over named functions in modern Javascript?
- 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?
- Which type of function do we use to create an instance of a class with some default values?
- If we want to inherit the properties of a parent class in a child class, what do we need to do?
- What's a common use of
finally
in error handling?
Exercises
- Write a while loop that iterates backwards through an array of numbers, logging out each element to the console.
- Write a function that takes in an array of strings and returns the shortest string in the array.
- 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"
. - 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}]
. - Write a function to count from a number
n
to zero without using a loop. - 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 callsconsole.log()
, and tap the button.