Skip to main content

Exercises

Recap questions

What is the syntax for including notes and explanations in our code?

You can instruct the machine to ignore the rest of the line by writing a double slash:

// this is a function
function greet() {
console.log('Hello Sally Brown');
}

greet(); // this is how to call it

You can comment the whole line, or just the text after the slashes.

Exercises

What does the following code do?

function greetSally() {
  console.log('Hello Sally Brown');
}

function greetCharlie() {
  console.log('Hello Charlie Brown');
}

function greetFriends() {
  console.log('Hello Sally Brown');
  console.log('Hello Charlie Brown');
}

greetCharlie();
greetFriends();
Toggle Console Output
  • 'Hello Charlie Brown'
  • 'Hello Sally Brown'
  • 'Hello Charlie Brown'
Without opening the results, what is the output of the code above?
  • First we call greetCharlie, that will print 'Hello Charlie Brown';
  • Then we call greetFriends, printing the remaining two logs.
Bonus: the code above can be written using just two instances of console.log, can you guess how?
function greetSally() {
console.log('Hello Sally Brown');
}

function greetCharlie() {
console.log('Hello Charlie Brown');
}

function greetFriends() {
greetSally();
greetCharlie();
}

greetCharlie();
greetFriends();

We haven't mentioned it explicitly yet, but you can call functions from inside functions! That will be the rule pretty soon. We'll get there together, don't fret!