如何在Chrome Tools中调试“未定义引号”错误?

时间:2017-06-08 17:16:29

标签: javascript html arrays

我一直在Chrome Tools中收到“引号未定义”的错误。我可以找到解决方案。当我点击让Chrome工具向我显示错误时,它会在Math.floor - Math.random中结束。

这是我的代码。

<div class="container-fluid text-center">
<h1>Random quote Generator</h1>


<br>
<button class="btn btn-danger" type="submit">New Quote</button>

<a href="#" id="tweet" class="btn btn-primary">Tweet Out!</a>
<br>
<div class="quotes">
    <span class="quote"></span>
    <span class="author"></span>
</div>

</div>

$(document).ready(function(){

function getQuote(){
var quotes = ["With the new day comes new strength and new thoughts.", "War is 
peace. Freedom is slavery. Ignorance is strength.", "The goal of education is 
the advancement of knowledge and the dissemination of truth.", "Success is 
getting what you want. Happiness is wanting what you get." , "You know an odd 
feeling? Sitting on the toilet eating a chocolate candy bar."];
var author = ["-Eleanor Roosevelt", "George Orwell", "John F. Kennedy", "Dale 
Carnegie", "George Carlin"];
};

  var randumNum = Math.floor((Math.random()*quotes.length));
  var randomQuote = quotes[randomNum];
  var randomAuthor = author[randomNum];

  $(".quotes").text(randomQuote);
  $(".author").text(randomAuthor);

  $(".btn").on("click", function(){
  getQuote();
  });

 });

3 个答案:

答案 0 :(得分:1)

您的引号变量在方法getQuotes()中定义,因此在外部不可见。我假设你的getQuote函数过早关闭,只需在设置作者后移动}

$(document).ready(function(){
 function getQuote(){
    var quotes = ["With the new day comes new strength and new thoughts.", "War is 
             peace. Freedom is slavery. Ignorance is strength.", "The goal of education is 
             the advancement of knowledge and the dissemination of truth.", "Success is 
             getting what you want. Happiness is wanting what you get." , "You know an odd 
            feeling? Sitting on the toilet eating a chocolate candy bar."];

     var author = ["-Eleanor Roosevelt", "George Orwell", "John F. Kennedy", "Dale 
Carnegie", "George Carlin"];

  var randumNum = Math.floor((Math.random()*quotes.length));
  var randomQuote = quotes[randomNum];
  var randomAuthor = author[randomNum];

  $(".quotes").text(randomQuote);
  $(".author").text(randomAuthor);
 };


 $(".btn").on("click", function(){
   getQuote();
  });
 });

答案 1 :(得分:0)

我认为您过早地结束了getQuote功能。你的代码应该是:

$(document).ready(function(){

function getQuote(){
var quotes = ["With the new day comes new strength and new thoughts.", "War is 
peace. Freedom is slavery. Ignorance is strength.", "The goal of education is 
the advancement of knowledge and the dissemination of truth.", "Success is 
getting what you want. Happiness is wanting what you get." , "You know an odd 
feeling? Sitting on the toilet eating a chocolate candy bar."];
var author = ["-Eleanor Roosevelt", "George Orwell", "John F. Kennedy", "Dale 
Carnegie", "George Carlin"];

  var randumNum = Math.floor((Math.random()*quotes.length));
  var randomQuote = quotes[randomNum];
  var randomAuthor = author[randomNum];

  $(".quotes").text(randomQuote);
  $(".author").text(randomAuthor);
};

  $(".btn").on("click", function(){
  getQuote();
  });

 });

答案 2 :(得分:0)

你的getQuote函数只关闭你在试图读取其值的函数之外定义2个变量'quotes'和'author',这是不可能的,所以这就是为什么你得到引号undefined,你需要关闭你的函数(}; )在这行代码之后

$(".author").text(randomAuthor);

$(document).ready(function(){
 function getQuote(){
    var quotes = ["With the new day comes new strength and new thoughts.", "War is peace. Freedom is slavery. Ignorance is strength.", "The goal of education is the advancement of knowledge and the dissemination of truth.", "Success is getting what you want. Happiness is wanting what you get." , "You know an odd feeling? Sitting on the toilet eating a chocolate candy bar."];

     var author = ["-Eleanor Roosevelt", "George Orwell", "John F. Kennedy", "Dale Carnegie", "George Carlin"];

  var randomNum = Math.floor((Math.random()*quotes.length));
  var randomQuote = quotes[randomNum];
  var randomAuthor = author[randomNum];

  $(".quotes").text(randomQuote);
  $(".author").text(randomAuthor);
 };


 $(".btn").on("click", function(){
   getQuote();
  });
 });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<button class="btn">click me and see</button>

<p class="quotes"></p>
<p><small class="author"></small></p>

编辑:继续你的片段并运行正常我们都提到的调整,但是你有一个名为'randumNum的变量然后你尝试将值读作'randomNum'(你和o)