function add(num1, num2) { }
return
. Example of returning the sum of two numbers:
function add(num1, num2) { return num1 + num2 }
let sum = add(2, 3)
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:
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:
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:
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
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:
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