[英]im trying to make a quiz for my bootcamp and i cant figure out what is wrong with the code
im trying to make a test for class and i cant seem to find the problem.我正在尝试对 class 进行测试,但我似乎找不到问题所在。
`function askQuestions(random){ `功能询问问题(随机){
var random = math.floor(math.random()*testQuestions.length);
var start = document.getElementById(testStart);
start.addEventListner('click', askQuestions);
if(start){
document.write(random);
} return; } 返回; }`
}`
this is the function and i have an array of objects, which is referenced in the "random" variable.这是 function,我有一个对象数组,在“随机”变量中引用。 im totally lost and i know im missing something simple.
我完全迷失了,我知道我错过了一些简单的东西。 this functions purpose is to display a random question from the array "testQuestions".
这个函数的目的是显示数组“testQuestions”中的一个随机问题。
Math is a global object and it starts with a capital M数学是一个全局 object,它以大写 M 开头
Math.floor
You are adding the event listener inside the function.您正在 function 中添加事件侦听器。 The function will only run if you call it, so the event listener will never be added.
function 只有在你调用它时才会运行,所以永远不会添加事件监听器。 Additionally,
document.write()
will overwrite the entire page.此外,
document.write()
将覆盖整个页面。 I changed it to use document.createElement()
and document.body.appendChild()
.我将其更改为使用
document.createElement()
和document.body.appendChild()
。 Change your code to this:将您的代码更改为:
function askQuestions(random) {
var random = Math.floor(Math.random() * testQuestions.length);
var el = document.createElement('p');
el.textContent = random.toString();
document.body.appendChild(el);
}
var start = document.getElementById(testStart);
start.addEventListner('click', askQuestions);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.