Let quotes = [ 'It is a truth universally acknowledged, that a single man in posse 'I hate to hear you talk about all women as if they were fine ladie 'Silly things do cease to be silly if they are done by sensible pec "Give a girl an education and introduce her properly into the worlc 'Life seems but a quick succession of busy nothings.", 'Our scars make us know that our past was for real.", 'I cannot speak well enough to be unintelligible.", 'One cannot be always laughing at a man without now and then stumbl 'Men were put into the world to teach women the law of compromise." ITho be it gentlomon orr ady krho hac

Programming Logic & Design Comprehensive
9th Edition
ISBN:9781337669405
Author:FARRELL
Publisher:FARRELL
Chapter11: More Object-oriented Programming Concepts
Section: Chapter Questions
Problem 2FTB
icon
Related questions
icon
Concept explainers
Question
100%

Having trouble with this debugging. new quotes should be visible everytime page loads up.

 

// Array of Jane Austen Quotes
let quotes = [
"It is a truth universally acknowledged, that a single man in possession of a good fortune, must be in want of a wife.",
"I hate to hear you talk about all women as if they were fine ladies instead of rational creatures. None of us want to be in calm waters all our lives.",
"Silly things do cease to be silly if they are done by sensible people in an impudent way.",
"Give a girl an education and introduce her properly into the world, and ten to one but she has the means of settling well, without further expense to anybody.",
"Life seems but a quick succession of busy nothings.",
"Our scars make us know that our past was for real.",
"I cannot speak well enough to be unintelligible.",
"One cannot be always laughing at a man without now and then stumbling on something witty.",
19
20
21
22
23
24
25
26
27
28
29
"Men were put into the world to teach women the law of compromise.",
30
"The person, be it gentlemen or lady, who has not pleasure in a good novel, must be intolerably stupid."
31
];
32
33
// Run the quote generator every time the page loads
window.addEventListener("load", quoteGenerator);
34
35
36
37
// Function to generate and display a random quote
function quoteGenerator() {
38
39
40
// Number of quotes in the array
quoteCount = quotes. length;
41
42
43
// Generate a random integer to select a quote
randomQuote = randomInt(0, quoteCount);
44
45
46
47
// Retrieve a randomly-selected quote
48
quotes = quotes[randomQuotes];
49
50
// Display the random quote
document.getElementByTagName("blockquote") [1].innerHTML = quotes;
51
52
53
}
54
55
56
57
58
59
60
61
62
/*:
// Function to return a randomly-selected integer between lowest and highest, inclusive
function randomInt(lowest, highest) {
63
64
65
let size = highest - lowest + 1;
return Math.floor(lowest + size*Math. random());
66
67
}
Transcribed Image Text:// Array of Jane Austen Quotes let quotes = [ "It is a truth universally acknowledged, that a single man in possession of a good fortune, must be in want of a wife.", "I hate to hear you talk about all women as if they were fine ladies instead of rational creatures. None of us want to be in calm waters all our lives.", "Silly things do cease to be silly if they are done by sensible people in an impudent way.", "Give a girl an education and introduce her properly into the world, and ten to one but she has the means of settling well, without further expense to anybody.", "Life seems but a quick succession of busy nothings.", "Our scars make us know that our past was for real.", "I cannot speak well enough to be unintelligible.", "One cannot be always laughing at a man without now and then stumbling on something witty.", 19 20 21 22 23 24 25 26 27 28 29 "Men were put into the world to teach women the law of compromise.", 30 "The person, be it gentlemen or lady, who has not pleasure in a good novel, must be intolerably stupid." 31 ]; 32 33 // Run the quote generator every time the page loads window.addEventListener("load", quoteGenerator); 34 35 36 37 // Function to generate and display a random quote function quoteGenerator() { 38 39 40 // Number of quotes in the array quoteCount = quotes. length; 41 42 43 // Generate a random integer to select a quote randomQuote = randomInt(0, quoteCount); 44 45 46 47 // Retrieve a randomly-selected quote 48 quotes = quotes[randomQuotes]; 49 50 // Display the random quote document.getElementByTagName("blockquote") [1].innerHTML = quotes; 51 52 53 } 54 55 56 57 58 59 60 61 62 /*: // Function to return a randomly-selected integer between lowest and highest, inclusive function randomInt(lowest, highest) { 63 64 65 let size = highest - lowest + 1; return Math.floor(lowest + size*Math. random()); 66 67 }
5. Use the debugger to locate the syntax and runtime errors within the document. Fix the errors using your code
editor.
6. Once you have fixed the syntax and runtime errors, continually reload the web page. Each time the page is
loaded, a Jane Austen quote is randomly selected from the array. Occasionally, an undefined value appears
where the quote should be. Find the source of this logic error by setting a breakpoint at Line 48 and watch the
values of the randomQuote and quotes [randomQuote] expression. What are their values that result in an
undefined quote?
7. Return to the code editor and fix the program to remove the logic error. (Hint: What is the largest index in the
quotes array and what would happen if you tried to retrieve an entry larger that index?)
8. Return to the project04-02.html file in your browser and continually reload it to verify that the undefined quote
no longer appears.
Transcribed Image Text:5. Use the debugger to locate the syntax and runtime errors within the document. Fix the errors using your code editor. 6. Once you have fixed the syntax and runtime errors, continually reload the web page. Each time the page is loaded, a Jane Austen quote is randomly selected from the array. Occasionally, an undefined value appears where the quote should be. Find the source of this logic error by setting a breakpoint at Line 48 and watch the values of the randomQuote and quotes [randomQuote] expression. What are their values that result in an undefined quote? 7. Return to the code editor and fix the program to remove the logic error. (Hint: What is the largest index in the quotes array and what would happen if you tried to retrieve an entry larger that index?) 8. Return to the project04-02.html file in your browser and continually reload it to verify that the undefined quote no longer appears.
Expert Solution
Step 1

Required HTML and JavaScript file codes are provided below with comments in code.

Text format of code for the JavaScript file project04-02.js to copy:

// code for javascript to have the strict mode on
"use strict";

/*    JavaScript 7th Edition
      Chapter 4
      Project 04-02

      Application to display a random Jane Austen Quote
      Author: John doe
      Date: 17/2/2022

      Filename: project04-02.js
*/

// Array of Jane Austen Quotes
let quotes = [
  "It is a truth universally acknowledged, that a single man in possession of a good fortune, must be in want of a wife.",
  "I hate to hear you talk about all women as if they were fine ladies instead of rational creatures. None of us want to be in calm waters all our lives.",
  "Silly things do cease to be silly if they are done by sensible people in an impudent way.",
  "Give a girl an education and introduce her properly into the world, and ten to one but she has the means of settling well, without further expense to anybody.",
  "Life seems but a quick succession of busy nothings.",
  "Our scars make us know that our past was for real.",
  "I cannot speak well enough to be unintelligible.",
  "One cannot be always laughing at a man without now and then stumbling on something witty.",
  "Men were put into the world to teach women the law of compromise.",
  "The person, be it gentlemen or lady, who has not pleasure in a good novel, must be intolerably stupid.",
];

// Run the quote generator every time the page loads
window.addEventListener("load", quoteGenerator);

// Function to generate and display a random quote
function quoteGenerator() {
  // Number of quotes in the array
  // Code Fix: declare the variable using "let" keyword
  let quoteCount = quotes.length; // is 10

  // Generate a random integer to select a quote
  // Code Fix: declare the variable using "let"
  // LOGICAL Error fix: Get value of random number from 0 to 9 instead of 0 to 10 because the array of "quotes" has 10 elements from index 0 to index 9
  // Hence, use randomInt(0, quoteCount - 1) instead of randomInt(0, quoteCount)
  let randomQuote = randomInt(0, quoteCount - 1);

  // Retrieve a randomly-selected quote
  // Code Fix: use "randomQuote" as the name of variable instead of "randomQuotes"
  // Code Fix: declare the variable using "let" keyword
  let quote = quotes[randomQuote];

  // console.log(randomQuote);

  // Display the random quote
  // Code Fix: It's getElementsByTagName() method instead of getElementByTagName() to get the <blockquote> element
  // Code Fix: Get first element by using [0] index instead of [1] since the 1st element of array is at index 0 in array
  document.getElementsByTagName("blockquote")[0].innerHTML = quote;
}

/*=================================================================*/
// Function to return a randomly-selected integer between lowest and highest, inclusive
function randomInt(lowest, highest) {
  let size = highest - lowest + 1;
  return Math.floor(lowest + size * Math.random());
}

Text format of code for the HTML file or webpage project04-02.html to copy:

<!DOCTYPE html>
<html>
  <head>
    <!--
      JavaScript 7th Edition
      Chapter 4
      Hands-on Project 4-2

      Author: John doe
      Date: 17/2/2022

      Filename: project04-02.html
   -->
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1.0" />
    <title>Hands-on Project 4-2</title>
    <link rel="stylesheet" href="styles.css" />
    <!-- js file (named project04-02.js) reference -->
    <script src="project04-02.js" defer></script>
  </head>

  <body>
    <header>
      <h1>Hands-on Project 4-2</h1>
    </header>

    <article>
      <h2>Random Quotes from Jane Austen</h2>
      <blockquote></blockquote>
    </article>
  </body>
</html>



trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 6 steps with 5 images

Blurred answer
Knowledge Booster
Control Structure
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage