我正在做测验,如果他回答正确,将其移至下一个问题,如果回答不正确,请放弃警告! 但我有点卡住 我找不到我错了! 对于for循环,我当然错了,但是我不知道哪一部分!
var questions = [{
question: "Best internet service provider is\n(a) Carrier\n\(b) AT&T\n(c) VIP/Telecom",
answer: "a"
},
{
questions: "Bad internet service provider is \n(a) VIP/Telecom\n\(b) AT&T\n(c) Carrier",
answer: "b"
},
]
for(var i = 0; i < questions.length; i++)
{
var starter = window.question(questions[i].question);
if(starter == questions[i].answer)
{
alert("LIKE");
}
else
{
alert("WRONG!");
}
}
答案 0 :(得分:1)
为什么要使用window.question
?只需使用您的问题var:
var questions = [{
question: "Best internet service provider is\n(a) Carrier\n\(b) AT&T\n(c) VIP/Telecom",
answer: "a"
},
{
questions: "Bad internet service provider is \n(a) VIP/Telecom\n\(b) AT&T\n(c) Carrier",
answer: "b"
},
]
for(var i = 0; i < questions.length; i++)
{
var starter = questions[i].question;
if(starter == questions[i].answer)
{
alert("LIKE");
}
else
{
alert("WRONG!");
}
}
答案 1 :(得分:1)
我认为您没有提供有关此问题的足够信息。但是,我假设您要提示用户回答您的问题,如果他们回答正确,您将显示“ LIKE”,否则为“ WRONG”,如下图所示。
基于此假设,这里是一个可行的解决方案:
var questions = [{
question: "Best internet service provider is\n(a) Carrier\n\(b) AT&T\n(c) VIP/Telecom",
answer: "a"
},
{
questions: "Bad internet service provider is \n(a) VIP/Telecom\n\(b) AT&T\n(c) Carrier",
answer: "b"
},
]
for (var i = 0; i < questions.length; i++) {
var starter = prompt(questions[i].question);
if (starter == questions[i].answer) {
alert("LIKE");
} else {
alert("WRONG!");
}
}
答案 2 :(得分:0)
我不确定我是否理解这个问题,因为您对问题的描述不甚理想。给定您提供的信息,我假设您正在构建html表单,并在用户正确回答的情况下使用javascript更改标记上的问题和答案选项。您发布的代码将永远无法使用,因为
// this is the question
// NOTE: the variable name should really be "question" or "currentQuestion", not "starter"
var starter = window.question(questions[i].question); // not sure why "window"
// this is the answer
questions[i].answer
// so
starter !== questions[i].answer will always be true
您需要的是将用户输入(我想象每个选项都有一个复选框)与答案的值进行比较,因此每个复选框都需要将其value
属性设置为一个不同的字母,然后进行比较答案正确。
所以您将需要这样的东西(在15m内完成操作只是为了给您一个想法。设计不是最好的-我建议您对此进行改进)。
var questions = [
{
question: 'Best internet service provider is:',
options: [
{
provider: 'Carrier',
value: 'a'
},
{
provider: 'AT&T',
value: 'b'
},
{
provider: 'VIP/Telecom',
value: 'c'
}
],
answer: 'a'
},
{
question: 'Worst internet service provider is:',
options: [
{
provider: 'VIP/Telecom',
value: 'a'
},
{
provider: 'AT&T',
value: 'b'
},
{
provider: 'Carrier',
value: 'c'
}
],
answer: 'b'
}
];
var counter = 0;
var questionSet = questions[counter];
var title = document.getElementById('title');
var form = document.getElementById('form');
const createTemplate = () => {
title.innerText = questionSet['question'];
form.innerHTML = getCheckboxes();
for (let i=0; i<form.elements.length; i++) {
let checkbox = form.elements[i];
checkbox.addEventListener('change', e => {
if (e.target.value === questionSet['answer']) {
counter++;
alert('You got it!');
if (counter < questions.length) {
questionSet = questions[counter];
createTemplate();
}
} else {
alert('Try again');
}
});
}
};
function getCheckboxes() {
let html = '';
questionSet['options'].forEach(item => {
html += `
<div>
<label>${item['value']}. ${item['provider']}<label>
<input type='checkbox' class='checkbox' value=${item['value']}>
<\div>
`;
});
return html;
}
createTemplate();
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Document</title>
</head>
<body>
<h1 id="title"></h1>
<form action="" id="form"></form>
</body>
</html>