Javascript
JavaScript Fundamentals -- Functions in JavaScript

Functions in JavaScript Exercise 1 Review

PRO
Outline

Here's one way you could have finished this exercise. These are just examples; your solutions may look a little different.

// 1. Write a named function that logs the "name" parameter to the console
function sayHello(name) {
  console.log('Hello ', name);
}
sayHello('Preston');

// 2. Write an arrow function that adds two numbers together using implicit return. Print the result to the console.
const add = (num1, num2) => num1 + num2;
const sum = add(4, 4);
console.log(sum);

// 3. Write an arrow function that subtracts two numbers, not using implicit return. Print the result to the console.
const subtract = (num1, num2) => {
  return num1 - num2;
}
const difference = subtract(10, 5);
console.log(difference);

// 4. Add a named function to the pizza object called "cook". Print a message to the console saying how long it will take to cook
const pizza = {
  name: 'Pepperoni',
  cook
};
function cook() {
  console.log(this.name + ' will take 10 minutes to cook');
}
pizza.cook();

// 5. Add an arrow function to the dog object that logs "bark, bark" to the console when called.
const dog = {
  name: 'Duke',
  bark: () => {
    console.log('bark, bark');
  }
};
dog.bark();


// 6. Add an anonymous function to the ice cream object called "eat" that logs a message saying the ice cream is being eaten
const iceCream = {
  name: 'vanilla',
  eat: function() {
    console.log(this.name + ' is being eaten');
  }
}
iceCream.eat(); 

You can see the answers to this exercise in this StackBlitz Project.

 

I finished! On to the next chapter