简体   繁体   中英

Why isn't this Javascript function printing into my HTML document?

I am trying to create an HTML page that outputs random Mark Twain quotes. I have the function and quotes array set up in a separate (linked) Javascript file. For whatever reason, I can't get the output to show up on the HTML document.

*Edit to add: My apologies for not doing better research & finding that case sensitivity matters in cases like these. I'm a newbie :)

Here is my Javascript code.

// Quotes
var quotes = [
["The size of a misfortune is not determinable by an outsider's measurement of it but only by the measurements applied to it by the person specially affected by it. The king's lost crown is a vast matter to the king but of no consequence to the child. The lost toy is a great matter to the child but in the king's eyes it is not a thing to break the heart about."],
["Trivial Americans go to Paris when they die."],
["There isn't time -- so brief is life -- for bickerings, apologies, heartburnings, callings to account. There is only time for loving -- & but an instant, so to speak, for that."],
["Thunder is good, thunder is impressive; but it is lightning that does the work."],
["Everyone is a moon, and has a dark side which he never shows to anybody."]
];

// Generate a random number from the array
function random_item(quotes) {
  return quotes[math.floor(math.random()*quotes.length)];
}

Here is my HTML code with the output that isn't working. I'm not including the full thing because this is the only relevant part.

<div id="quotes">
   <script>
   document.write(random_item(quotes));
   </script>
   </div>

If it were working, each time the page is visited/refreshed, one of the quotes should display at random. They won't show up at all though.

you have a typo with the Math object.

And consider avoiding putting script inside div

 // Quotes var quotes = [ ["The size of a misfortune is not determinable by an outsider's measurement of it but only by the measurements applied to it by the person specially affected by it. The king's lost crown is a vast matter to the king but of no consequence to the child. The lost toy is a great matter to the child but in the king's eyes it is not a thing to break the heart about."], ["Trivial Americans go to Paris when they die."], ["There isn't time -- so brief is life -- for bickerings, apologies, heartburnings, callings to account. There is only time for loving -- & but an instant, so to speak, for that."], ["Thunder is good, thunder is impressive; but it is lightning that does the work."], ["Everyone is a moon, and has a dark side which he never shows to anybody."] ]; // Generate a random number from the array function random_item(quotes) { return quotes[Math.floor(Math.random()*quotes.length)]; } document.querySelector('#quotes').innerText = random_item(quotes); // use instead of document.write
 <div id="quotes"> </div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM