Exercise Set 7

Exercise 1 of 6

Watch the array video. Add the text box value to the array when the button is clicked.

HTML

<input type="text" placeholder="word" value="hello" id="wordInput">
<button id="addWordButton">Add word</button>
<p id="wordsParagraph"></p>

JavaScript

var wordInput = document.getElementById("wordInput");
var addWordButton = document.getElementById("addWordButton");
var wordsParagraph = document.getElementById("wordsParagraph");

var words = [];

addWordButton.addEventListener("click", addWord);

function addWord() {
  // your code will appear here
  wordsParagraph.innerHTML = words;
}

You must be signed in to type your code.

Exercise 2 of 6

Add the number to the array when the button is clicked.

HTML

<button id="addNumberButton">Add number</button>
<p id="numbersParagraph"></p>

JavaScript

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

var number = 0;
var numbers = [];

addNumberButton.addEventListener("click", addNumber);

function addNumber() {
  number = number + 1;
  // your code will appear here
  numbersParagraph.innerHTML = numbers;
}

You must be signed in to type your code.

Exercise 3 of 6

Add the random number to the array when the button is clicked.

HTML

<button id="addRandomNumberButton">Add random number</button>
<p id="randomNumbersParagraph"></p>

JavaScript

var addRandomNumberButton = document.getElementById("addRandomNumberButton");
var randomNumbersParagraph = document.getElementById("randomNumbersParagraph");

var randomNumbers = [];

addRandomNumberButton.addEventListener("click", addRandomNumber);

function addRandomNumber() {
  var randomNumber = Math.floor(Math.random() * 10);
  // your code will appear here
  randomNumbersParagraph.innerHTML = randomNumbers;
}

You must be signed in to type your code.

Exercise 4 of 6

Show the first word in the array when the button is clicked.

HTML

<button id="showFirstWordButton">Show first word</button>
<p id="firstWordParagraph"></p>

JavaScript

var showFirstWordButton = document.getElementById("showFirstWordButton");
var firstWordParagraph = document.getElementById("firstWordParagraph");

var words = ["first", "second", "third"];

showFirstWordButton.addEventListener("click", showFirstWord);

function showFirstWord() {
  // your code will appear here
}

You must be signed in to type your code.

Exercise 5 of 6

Show the third word in the array when the button is clicked.

HTML

<button id="showThirdWordButton">Show third word</button>
<p id="thirdWordParagraph"></p>

JavaScript

var showThirdWordButton = document.getElementById("showThirdWordButton");
var thirdWordParagraph = document.getElementById("thirdWordParagraph");

var words = ["first", "second", "third"];

showThirdWordButton.addEventListener("click", showThirdWord);

function showThirdWord() {
  // your code will appear here
}

You must be signed in to type your code.

Exercise 6 of 6

Show a random word in the array when the button is clicked.

HTML

<button id="showRandomWordButton">Show random word</button>
<p id="randomWordParagraph"></p>

JavaScript

var showRandomWordButton = document.getElementById("showRandomWordButton");
var randomWordParagraph = document.getElementById("randomWordParagraph");

var words = ["first", "second", "third"];

showRandomWordButton.addEventListener("click", showRandomWord);

function showRandomWord() {
  var randomNumber = Math.floor(Math.random() * words.length);
  // your code will appear here
}

You must be signed in to type your code.