Standard Exercises

0. Given the following code, what do you expect to be logged by the bottom of the script? (Type it into the console to try it out!).

var myObject = {}
myObject.sayHello = function() {
    console.log("Hello");
}

myObject.sayHello();
myObject.sayHello();

1. Given the following code, what do you expect to be logged by the bottom of the script? (Type it into the console to try it out!).

var myScore = {
    value: 0
}

myScore.increment = function() {
    this.value += 1;
}

myScore.increment();
myScore.increment();

console.log(myScore.value);

2. Create an empty object and add a function to it which prints "Hello World" to the console. Call the function on the line below.

3. Add a function to this object called logFullName() which will console.log the firstName and lastName to the console. (Tip. You will need to use 'this' to access firstName and lastName).

var myName = {
    firstName: "John",
    lastName: "Wordsworth"
}

// Add your function to myName here

myName.logFullName();

Advanced Exercises

4. Create an object which represents a recipe. The object should have a title and a list of ingredients (an array of strings). Then add a method to the object logEntireRecipe() which will print the title and ingredients for the recipe.

var recipe = {
    title: ...
    ingredients: ...
}

recipe.logEntireRecipe = function() {
    ...
}

recipe.logEntireRecipe();

5. Create an object which stores a list of all books you would like to read. Each book should be another object, with a 'title' and 'author' property. The object should have 2 methods - 'addBook(title, author) which creates a new object and adds it to the reading list and 'logList' which will print the whole list out.

var readingList = {
    var books = [];
}

readingList.addBook = function(title, author) {
    ...
}

readingList.logList = function() {
    ...
}

readingList.addBook("Harry Potter", "J.K. Rowling");
readingList.addBook("Game of Thrones", "George R. R. Martin");
readingList.logList();
console.log("Hello World");

    
Hint: Alternatively, just hold "shift" and press "enter" to run your code.