【问题标题】:Console returning a random word from an array, putting word in the console控制台从数组中返回一个随机单词,将单词放入控制台
【发布时间】:2014-05-02 20:58:01
【问题描述】:

您好,我正在尝试从数组中返回一个随机单词。一切正常,但屏幕上打印的问题有没有办法在命令提示框中显示数组中的随机单词?

这是我的代码,它可以工作,但问题出现在屏幕上而不是控制台中。有什么办法吗??

    var NorthAmericaQuestions = ["How many States are in USA? Are There 51, 52, 54,           50?","How big are Dictionaries? Huge, Okayish, Thin?"]

    var NorthAmericaAnswers = [52,"Huge"]

    function RandomGen() {
        randno = Math.floor ( Math.random() * NorthAmericaQuestions.length ); 
        document.write(NorthAmericaQuestions[randno]);
}

    Q1 = prompt (RandomGen(), "Type Here!");
    if (Q1 == NorthAmericaAnswers[0]){
        document.write("<br>Thats Correct!!");
}
    else {
        document.write ("<br>Oops Your Wrong!!");
}

【问题讨论】:

  • console.log(NorthAmericaQuestions[randno]) 不是document.write(NorthAmericaQuestions[randno]);?
  • 嗨,感谢您的帮助,但是当放置 console.log 而不是 document.write 时,它​​不会出现在网页上,这很好,但我如何让它出现在提示框中他们回答问题???
  • 你不能使用这个方法。如果您想弹出答案,请改用alertalert('Answer correct!!')。否则考虑使用分层的divs 来产生你想要的效果。

标签: javascript arrays dom console document


【解决方案1】:

因为你写的是文档而不是控制台

document.write(NorthAmericaQuestions[randno]);

它会写在文件上

要在控制台上书写,您必须使用

console.log(NorthAmericaQuestions[randno]);

【讨论】:

  • 嗨,感谢您的帮助,但是当放置 console.log 而不是 document.write 时,它​​不会出现在网页上,这很好,但我如何让它出现在提示框中他们回答问题???
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-20
  • 2019-05-05
相关资源
最近更新 更多