Defining Function

To customize a function's behavior, put variable names in the parentheses, which are called parameters. Example of a function that can add any two numbers:
function add(num1, num2) {

}
To have the function return a result, use the keyword return. Example of returning the sum of two numbers:
function add(num1, num2) {
  return num1 + num2
}
When calling a function, pass in arguments for the parameters, and store the return value in a variable. Example of calling the function in the example above:
let sum = add(2, 3)

Exercise 1 of 6

Call the add function and store the return value in sum.

HTML

<button id="sumButton">Show sum</button>
<p id="sumParagraph">Sum:</p>

JavaScript

let sumButton = document.getElementById(`sumButton`)
let sumParagraph = document.getElementById(`sumParagraph`)

sumButton.addEventListener(`click`, showSum)

function showSum() {
  // your code will appear here
  sumParagraph.innerHTML = `Sum: ${sum}`
}

function add(num1, num2) {
  return num1 + num2
}

Your code

Test your code

Sum:

Exercise 2 of 6

Write the parameters for the subtract function so that the result is 2.

HTML

<button id="differenceButton">Show difference</button>
<p id="differenceParagraph">Difference:</p>

JavaScript

let differenceButton = document.getElementById(`differenceButton`)
let differenceParagraph = document.getElementById(`differenceParagraph`)

differenceButton.addEventListener(`click`, showDifference)

function showDifference() {
  let difference = subtract(5, 3)
  differenceParagraph.innerHTML = `Difference: ${difference}`
}

function subtract(// your code will appear here) {
  return num1 - num2
}

Your code

Test your code

Difference:

Exercise 3 of 6

Write the return statement for the multiply function so that the result is 6.

HTML

<button id="productButton">Show product</button>
<p id="productParagraph">Product:</p>

JavaScript

let productButton = document.getElementById(`productButton`)
let productParagraph = document.getElementById(`productParagraph`)

productButton.addEventListener(`click`, showProduct)

function showProduct() {
  let product = multiply(2, 3)
  productParagraph.innerHTML = `Product: ${product}`
}

function multiply(num1, num2) {
  // your code will appear here
}

Your code

Test your code

Product:

Exercise 4 of 6

Call the isPositive function.

HTML

<button id="positiveButton">Show is positive</button>
<p id="positiveParagraph"></p>

JavaScript

let positiveButton = document.getElementById(`positiveButton`)
let positiveParagraph = document.getElementById(`positiveParagraph`)

positiveButton.addEventListener(`click`, showPositive)

function showPositive() {
  if (// your code will appear here) {
    positiveParagraph.innerHTML = `The number is positive`
  }
  else {
    positiveParagraph.innerHTML = `The number is not positive`
  }
}

function isPositive(num) {
  return num > 0
}

Your code

Test your code

Exercise 5 of 6

Write the parameters for the slice function so that the result is one, two, three.

HTML

<button id="sliceButton">Show slice of array</button>
<p id="sliceParagraph">Slice of array:</p>

JavaScript

let sliceButton = document.getElementById(`sliceButton`)
let sliceParagraph = document.getElementById(`sliceParagraph`)

sliceButton.addEventListener(`click`, showSlice)

function showSlice() {
  let sliceOfArray = slice([`zero`, `one`, `two`, `three`, `four`], 1, 4)
  sliceParagraph.innerHTML = `Slice of array: ${sliceOfArray.join(`, `)}`
}

// returns a portion of the array from a start index to an end index (end not included)
function slice(// your code will appear here) {
  let resultArray = []

  for (let i = start; i < end; i = i + 1) {
    resultArray.push(array[i])
  }

  return resultArray
}

Your code

Test your code

Slice of array:

Exercise 6 of 6

Write the appropriate return statement for the includes function.

HTML

<button id="includesButton">Show includes</button>
<p id="includesParagraph"></p>

JavaScript

let includesButton = document.getElementById(`includesButton`)
let includesParagraph = document.getElementById(`includesParagraph`)

includesButton.addEventListener(`click`, showIncludes)

function showIncludes() {
  if (includes([1, 1, 2, 3, 5], 3)) {
    includesParagraph.innerHTML = `The array includes the value` 
  }
  else {
    includesParagraph.innerHTML = `The array does not include the value` 
  }
}

// returns true if the array contains the value, or false if it does not
function includes(array, valueToFind) {
  for (let item in array) {
    if (item == valueToFind) {
      // your code will appear here
    }
  }

  return false
}

Your code

Test your code