Exercise Set 11

Exercise 1 of 6

Call the add function and store the return value. Read about calling functions.

HTML

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

JavaScript

var showSumButton = document.getElementById("showSumButton");
var sumParagraph = document.getElementById("sumParagraph");

showSumButton.addEventListener("click", showSum);

function showSum() {
  // your code will appear here
  sumParagraph.innerHTML = "Sum: " + sum;
}

function add(number1, number2) {
  return number1 + number2;
}

You must be signed in to type your code.

Exercise 2 of 6

Write the parameters for the subtract function. Read about defining functions.

HTML

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

JavaScript

var showDifferenceButton = document.getElementById("showDifferenceButton");
var differenceParagraph = document.getElementById("differenceParagraph");

showDifferenceButton.addEventListener("click", showDifference);

function showDifference() {
  var difference = subtract(5, 3);
  differenceParagraph.innerHTML = "Difference: " + difference;
}

function subtract(// your code will appear here) {
  return number1 - number2;
}

You must be signed in to type your code.

Exercise 3 of 6

Write the return statement for the multiply function. Read about the return statement.

HTML

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

JavaScript

var showProductButton = document.getElementById("showProductButton");
var productParagraph = document.getElementById("productParagraph");

showProductButton.addEventListener("click", showProduct);

function showProduct() {
  var product = multiply(2, 3);
  productParagraph.innerHTML = "Product: " + product;
}

function multiply(number1, number2) {
  // your code will appear here
}

You must be signed in to type your code.

Exercise 4 of 6

Call the isStringEmpty function and store the return value.

HTML

<button id="showStringEmptyButton">Show string empty</button>
<p id="stringEmptyParagraph">String empty:</p>

JavaScript

var showStringEmptyButton = document.getElementById("showStringEmptyButton");
var stringEmptyParagraph = document.getElementById("stringEmptyParagraph");

showStringEmptyButton.addEventListener("click", showStringEmpty);

function showStringEmpty() {
  // your code will appear here
  stringEmptyParagraph.innerHTML = "String empty: " + stringEmpty;
}

function isStringEmpty(string) {
  if (string == "") {
    return true;
  }
  else {
    return false;
  }
}

You must be signed in to type your code.

Exercise 5 of 6

Write the parameters for the areStringsEqual function.

HTML

<button id="showStringsEqualButton">Show strings equal</button>
<p id="stringsEqualParagraph">Strings equal:</p>

JavaScript

var showStringsEqualButton = document.getElementById("showStringsEqualButton");
var stringsEqualParagraph = document.getElementById("stringsEqualParagraph");

showStringsEqualButton.addEventListener("click", showStringsEqual);

function showStringsEqual() {
  var stringsEqual = areStringsEqual("cat", "dog");
  stringsEqualParagraph.innerHTML = "Strings equal: " + stringsEqual;
}

function areStringsEqual(// your code will appear here) {
  if (string1 == string2) {
    return true;
  }
  else {
    return false;
  }
}

You must be signed in to type your code.

Exercise 6 of 6

Write a return statement for the doesArrayContainNumber function.

HTML

<button id="showArrayContainsNumberButton">Show array contains number</button>
<p id="arrayContainsNumberParagraph">Array contains number:</p>

JavaScript

var showArrayContainsNumberButton = document.getElementById("showArrayContainsNumberButton");
var arrayContainsNumberParagraph = document.getElementById("arrayContainsNumberParagraph");

showArrayContainsNumberButton.addEventListener("click", showArrayContainsNumber);

function showArrayContainsNumber() {
  var arrayContainsNumber = doesArrayContainNumber([1, 1, 2, 3, 5], 3);
  arrayContainsNumberParagraph.innerHTML = "Array contains number: " + arrayContainsNumber;
}

function doesArrayContainNumber(array, number) {
  for (var i = 0; i < array.length; i++) {
    if (array[i] == number) {
      // your code will appear here
    }
  }

  return false;
}

You must be signed in to type your code.