Как использовать JavaScript для генерации случайных цитат на сайте? - PullRequest
0 голосов
/ 12 ноября 2018

Ниже приведен код, который у меня есть.Проблема, с которой я столкнулся, заключается в том, что там, где кавычки должны отображаться на веб-странице, он просто говорит «неопределенный».Я, честно говоря, не уверен, откуда проблема.

var randomQ = randomInt(0, 10);

function randomInt(lowest, size) {
    Math.floor(Math.random() * size) + lowest;
    return randomQ;
}

var quoteElem = document.getElementsByTagName("quote")[0];

quoteElem.innerHTML = getQuote(randomQ);

function getQuote(n) {
   var 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."
   ];
   
   return quotes[n];
}

1 Ответ

0 голосов
/ 12 ноября 2018

Вы должны научиться читать свои ошибки. undefined что?

"message": "Uncaught TypeError: Невозможно установить свойство 'innerHTML'

Это означает, что вы пытаетесь установить innerHTML элемента, который не может быть найден / не существует.

var quoteElem = document.getElementsByTagName("quote")[0];

В HTML нет тега элемента под названием «цитата». Возможно, вы имеете в виду элемент с идентификатором «цитата»?

Во-вторых, ваша функция с именем randomInt() не возвращала сгенерированное вами случайное число, а, скорее, некоторую неопределенную переменную с именем randomQ

var randomQ = randomInt(0, 10);
function randomInt(lowest, size) {
  //Return the actual value instead
  return Math.floor(Math.random() * size) + lowest; 

  //return randomQ  <-- what is this? This is what is undefined
}

var quoteElem = document.getElementById("quote");

quoteElem.innerHTML = getQuote(randomQ);

function getQuote(n) {
   var 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."
   ];
   
   return quotes[n];
}
<div id="quote"></div>
...