Exercise Set 8

Exercise 1 of 6

Watch the loop video. Write the initialization to show the numbers 0 through 9.

HTML

<button id="showNumbersButton">Show numbers 0 through 9</button>
<p id="numbersParagraph">Numbers:</p>

JavaScript

var showNumbersButton = document.getElementById("showNumbersButton");
var numbersParagraph = document.getElementById("numbersParagraph");

showNumbersButton.addEventListener("click", showNumbers);

function showNumbers() {
  for (// your code will appear here; i < 10; i++) {
    numbersParagraph.innerHTML = numbersParagraph.innerHTML + " " + i;
  }
}

You must be signed in to type your code.

Exercise 2 of 6

Write the initialization to show the numbers 10 through 19.

HTML

<button id="showHigherNumbersButton">Show numbers 10 through 19</button>
<p id="higherNumbersParagraph">Numbers:</p>

JavaScript

var showHigherNumbersButton = document.getElementById("showHigherNumbersButton");
var higherNumbersParagraph = document.getElementById("higherNumbersParagraph");

showHigherNumbersButton.addEventListener("click", showHigherNumbers);

function showHigherNumbers() {
  for (// your code will appear here; i < 20; i++) {
    higherNumbersParagraph.innerHTML = higherNumbersParagraph.innerHTML + " " + i;
  }
}

You must be signed in to type your code.

Exercise 3 of 6

Write the condition to say hello 10 times.

HTML

<button id="sayHelloButton">Say hello 10 times</button>
<p id="helloParagraph">Words:</p>

JavaScript

var sayHelloButton = document.getElementById("sayHelloButton");
var helloParagraph = document.getElementById("helloParagraph");

sayHelloButton.addEventListener("click", sayHello);

function sayHello() {
  for (var i = 0; // your code will appear here; i++) {
    helloParagraph.innerHTML = helloParagraph.innerHTML + " hello";
  }
}

You must be signed in to type your code.

Exercise 4 of 6

Write the condition to show the numbers 0 through 9 in reverse order.

HTML

<button id="showReverseNumbersButton">Show numbers 0 through 9 in reverse</button>
<p id="reverseNumbersParagraph">Numbers:</p>

JavaScript

var showReverseNumbersButton = document.getElementById("showReverseNumbersButton");
var reverseNumbersParagraph = document.getElementById("reverseNumbersParagraph");

showReverseNumbersButton.addEventListener("click", showReverseNumbers);

function showReverseNumbers() {
  for (var i = 9; // your code will appear here; i--) {
    reverseNumbersParagraph.innerHTML = reverseNumbersParagraph.innerHTML + " " + i;
  }
}

You must be signed in to type your code.

Exercise 5 of 6

Write the iteration to show the even numbers between 0 and 19.

HTML

<button id="showEventNumbersButton">Show even numbers between 0 and 19</button>
<p id="evenNumbersParagraph">Numbers:</p>

JavaScript

var showEvenNumbersButton = document.getElementById("showEvenNumbersButton");
var evenNumbersParagraph = document.getElementById("evenNumbersParagraph");

showEvenNumbersButton.addEventListener("click", showEvenNumbers);

function showEvenNumbers() {
  for (var i = 0; i < 20; // your code will appear here) {
    evenNumbersParagraph.innerHTML = evenNumbersParagraph.innerHTML + " " + i;
  }
}

You must be signed in to type your code.

Exercise 6 of 6

Show the squares of 0 through 9. Refer to this squares table.

HTML

<button id="showSquaresButton">Show squares of 0 through 9</button>
<p id="squaresParagraph">Squares:</p>

JavaScript

var showSquaresButton = document.getElementById("showSquaresButton");
var squaresParagraph = document.getElementById("squaresParagraph");

showSquaresButton.addEventListener("click", showSquares);

function showSquares() {
  for (var i = 0; i < 10; i++) {
    // your code will appear here
  }
}

You must be signed in to type your code.