简体   繁体   English

我试图为我的训练营做一个测验,但我不知道代码有什么问题

[英]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.

相关问题 我是一个新学习者,无法弄清楚为什么我的代码不起作用 - im a new learner and cant figure out why my code is not working 我的捕获错误,我似乎无法弄清楚 - Im getting an error with my catch that i cant seem to figure out 我正在尝试找出此Javascript代码出了什么问题 - I am trying to figure out what is wrong with this Javascript code 无法弄清楚我做错了什么。 未处理的拒绝(TypeError):无法读取未定义的属性“检查” - Cant figure out what Im doing wrong. Unhandled Rejection (TypeError): Cannot read property 'inspection' of undefined 我的桌子看起来很奇怪,无法弄清楚我在 javascript 上做错了什么 - my table is looking weird and cant figure out what i did wrong on the javascript 我正在尝试添加用户提示以添加到我拥有的表中,但我无法弄清楚 - im trying to add a user prompt to add into the table i have but i cant figure it out 我的代码有一个简单的问题,我无法弄清楚 - My code has a simple issue that i cant figure out 我想弄清楚这里出了什么问题 - I am trying to figure out what is wrong here 无法弄清楚我的JavaScript代码出了什么问题 - Can't figure out what is wrong with my JavaScript code 试图弄清楚如何使我的代码使用html按钮触发 - Trying to figure out how to make my code trigger with html buttons
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM