gpt4 book ai didi

Javascript 测验,因此当它达到完整的问题循环时就会停止

转载 作者:行者123 更新时间:2023-12-01 03:38:13 24 4
gpt4 key购买 nike

这听起来像是我之前问过的问题,但它只得到了部分回答。所以我想从我提出的问题开始。然后,一旦到达最后一个问题(#5),它就会返回到第一个问题(#1),当到达起始问题时,它将停止。我尝试在以下代码中执行此操作,但它不起作用。我今年 11 岁,大约 9 个月前开始编程。

<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Are you smarter than a 5th Grader?!!</title>
<link rel="stylesheet" href="style.css">
</head>

<body onload="starter()" bgcolor="lightblue">
<h1><marquee><font color="red">Make Trivia Great Again!</font></marquee></h1>
<h2><em><center>Are You Smater Than a 5th Grader?</center></em></h2>
<button onclick="sc()">Start</button>
<br>
<p id="demo"> </p>
<div id="result"></div>
<button onclick="reset()">Reset Score</button>
<script>
function starter() {
setTimeout("clickCounter()", 100)
setTimeout("minusCounter()", 101)
}

function reset() {
setTimeout("clickCounter()", 100)
localStorage.clickcount = -1

}

function clickCounter() {
if (typeof(Storage) !== "undefined") {

if (localStorage.clickcount) {
localStorage.clickcount = Number(localStorage.clickcount) + 1;
} else {
localStorage.clickcount = 0;
}
document.getElementById("result").innerHTML = "Score:" + localStorage.clickcount
} else {}
}

function minusCounter() {

if (typeof(Storage) !== "undefined") {

if (localStorage.clickcount) {
localStorage.clickcount = Number(localStorage.clickcount) - 1;
} else {
localStorage.clickcount = 0;
}
document.getElementById("result").innerHTML = "Score:" + localStorage.clickcount
} else {}
}

if (!("scramble" in Array.prototype)) {
Object.defineProperty(Array.prototype, "scramble", {
enumerable: false,
value: function() {
var o, i, ln = this.length;
while (ln--) {
i = Math.random() * (ln + 1) | 0;
o = this[ln];
this[ln] = this[i];
this[i] = o;
}
return this;


}
});
}
var quiz = [{
"question": "When was the first apple computer made?",
"choices": ["1904", "1976", "1978", "2004"],
"correct": ["1976"]
}, {
"question": "Who is the founder of Microsoft?",
"choices": ["Bill Gates", "Steve Jobs", "Steve Wozniak", "Martin Shaba"],
"correct": "Bill Gates"
}, {
"question": "What was your first dream?",
"choices": ["8 bits", "64 bits", "1024 bits"],
"correct": "8 bits"
}, {
"question": "The C programming language was developed by?",
"choices": ["Brendan Eich", "Dennis Ritchie", "Guido van Rossum"],
"correct": "Dennis Ritchie"
}, {
"question": "What does CC mean in emails?",
"choices": ["Carbon Copy", "Creative Commons", "other"],
"correct": "Carbon Copy"
}, {
"question": "What is the full for of IP",
"choices": ["Internet provider", "Intenet Port", "Other", "Internet Protocol"],
"correct": "Other"
}]

function stop() {
alert("stopped")
}

function setter() {
for (x = 1; x < 7; x++) {
uc()
}
}

function sc() {
var q = prompt("start question #", "")
if (q >= 6) {
alert("please pick a valid question")
sc()
} else if (q <= 5 && q > 0) {
ec()
} else if (q == 0) {
alert("please pick a valid question")
sc()
return;

} else {
alert("Please pick a valid question"), sc()
}

function ec() {
for (x = q; x < 7; x++) {
if (x == 6) {
setter()

} else if (x == x) {
return;
} else {
uc()
}
}

function uc() {
quiz.forEach(q => q.choices.scramble());
var ans = ""

function myFunction(item, index) {
ans += "\n[" + (index + 1) + "]: " + item;
}
quiz[x].choices.forEach(myFunction);
var y = prompt(quiz[x].question + "\nYour anwser is:" + ans);
if (y == quiz[x].correct) {
alert("Correct!")
clickCounter()
} else if (y == "Cancel") {
alert("canceled")
page.reload()
} else {
alert("Wrong! Please Try Again.");
repeat()
}

function repeat() {
quiz.forEach(q => q.choices.scramble());
var ans = ""

function myFunction(item, index) {
ans += "\n[" + (index + 1) + "]: " + item;
}
quiz[x].choices.forEach(myFunction);
var y = prompt(quiz[x].question + "\nYour anwser is:" + ans);
if (y == quiz[x].correct) {
alert("Correct!,Good Job")
clickCounter()
} else if (y == "Cancel") {
alert("canceled")
} else {
alert("Sorry! \nThe right answer is " + quiz[x].correct);
}
}
}
}
}

</script>


</body>

</html>

最佳答案

对于一个 11 岁的 child 来说,这是非常令人印象深刻的编码。您在这里面临的问题是函数和变量范围。函数在不应该的情况下被嵌套在其他函数中。此外,当您使用在其他函数中使用的变量时,您必须全局声明它(尽管有时这可能是一个坏主意)。

从这里开始了解有关范围的更多信息:https://www.w3schools.com/js/js_scope.asp

继续编码:)

您的代码已修复如下:

<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Are you smarter than a 5th Grader?!!</title>
<link rel="stylesheet" href="style.css">
</head>

<body onload="starter()" bgcolor="lightblue">
<h1><marquee><font color="red">Make Trivia Great Again!</font></marquee></h1>
<h2><em><center>Are You Smater Than a 5th Grader?</center></em></h2>
<button onclick="sc()">Start</button>
<br>
<p id="demo"> </p>
<div id="result"></div>
<button onclick="reset()">Reset Score</button>
<script>

function starter() {
setTimeout("clickCounter()", 100)
setTimeout("minusCounter()", 101)
}

function reset() {
setTimeout("clickCounter()", 100)
localStorage.clickcount = -1

}

function clickCounter() {
if (typeof(Storage) !== "undefined") {

if (localStorage.clickcount) {
localStorage.clickcount = Number(localStorage.clickcount) + 1;
} else {
localStorage.clickcount = 0;
}
document.getElementById("result").innerHTML = "Score:" + localStorage.clickcount
} else {}
}

function minusCounter() {

if (typeof(Storage) !== "undefined") {

if (localStorage.clickcount) {
localStorage.clickcount = Number(localStorage.clickcount) - 1;
}
else {
localStorage.clickcount = 0;
}

document.getElementById("result").innerHTML = "Score:" + localStorage.clickcount
} else {

}
}

if (!("scramble" in Array.prototype)) {
Object.defineProperty(Array.prototype, "scramble", {
enumerable: false,
value: function() {
var o, i, ln = this.length;
while (ln--) {
i = Math.random() * (ln + 1) | 0;
o = this[ln];
this[ln] = this[i];
this[i] = o;
}
return this;


}
});
}

var quiz = [{
"question": "When was the first apple computer made?",
"choices": ["1904", "1976", "1978", "2004"],
"correct": ["1976"]
}, {
"question": "Who is the founder of Microsoft?",
"choices": ["Bill Gates", "Steve Jobs", "Steve Wozniak", "Martin Shaba"],
"correct": "Bill Gates"
}, {
"question": "What was your first dream?",
"choices": ["8 bits", "64 bits", "1024 bits"],
"correct": "8 bits"
}, {
"question": "The C programming language was developed by?",
"choices": ["Brendan Eich", "Dennis Ritchie", "Guido van Rossum"],
"correct": "Dennis Ritchie"
}, {
"question": "What does CC mean in emails?",
"choices": ["Carbon Copy", "Creative Commons", "other"],
"correct": "Carbon Copy"
}, {
"question": "What is the full for of IP",
"choices": ["Internet provider", "Intenet Port", "Other", "Internet Protocol"],
"correct": "Other"
}];

function stop() {
alert("stopped")
}

function setter() {
for (x = 1; x < 7; x++) {
uc();
}
}


var q;
function sc() {
q = prompt("start question #", "")
if (q >= 6) {
alert("please pick a valid question")
sc()
} else if (q <= 5 && q > 0) {
ec()
} else if (q == 0) {
alert("please pick a valid question")
sc()
return;

} else {
alert("Please pick a valid question"), sc()
}
}

var x;
function ec() {
console.log('ec');
for (x = q; x < 7; x++) {
if (x == 6) {
setter()

//} else if (x == x) {
// return;
} else {
uc()
}
}
}

function uc() {
console.log('uc');
quiz.forEach(q => q.choices.scramble());
var ans = ""

function myFunction(item, index) {
ans += "\n[" + (index + 1) + "]: " + item;
}
quiz[x].choices.forEach(myFunction);
var y = prompt(quiz[x].question + "\nYour anwser is:" + ans);
if (y == quiz[x].correct) {
alert("Correct!")
clickCounter()
} else if (y == "Cancel") {
alert("canceled")
page.reload()
} else {
alert("Wrong! Please Try Again.");
repeat()
}
}

function repeat() {
quiz.forEach(q => q.choices.scramble());
var ans = ""

function myFunction(item, index) {
ans += "\n[" + (index + 1) + "]: " + item;
}
quiz[x].choices.forEach(myFunction);
var y = prompt(quiz[x].question + "\nYour anwser is:" + ans);
console.log(y);
console.log(quiz[x].correct);
if (y == quiz[x].correct) {
alert("Correct!,Good Job")
clickCounter()
} else if (y == "Cancel") {
alert("canceled")
} else {
alert("Sorry! \nThe right answer is " + quiz[x].correct);
}
}


</script>


</body>

</html>

关于Javascript 测验,因此当它达到完整的问题循环时就会停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44091902/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com