gpt4 book ai didi

javascript - 有没有办法简化这段 JavaScript 代码?

转载 作者:行者123 更新时间:2023-12-02 09:05:18 24 4
gpt4 key购买 nike

我知道这是一个广泛的问题,但我仍然是新手,不知道要搜索/询问什么。

我有一个 Jeopardy 游戏的 JavaScript 系列。我确信有更好的方法来做到这一点,因为有太多的重复。我正在考虑一些变量、数组和/或在调用函数时传递 ID,但不知道从哪里开始。任何想法,将不胜感激。我并不是要求任何人做这项工作,只是给我一些想法和例子,告诉我该往哪个方向走。

var score = 0;


//Disable the question button
function disableButton(btnID){
document.getElementById(btnID.id).disabled = true;
}

//Show current score
function endQuestion(){
alert ("Your total score is now " +score);
}

//Toggle Images On
function showImgBtn1(){
document.getElementById('btn1pic').style.visibility = 'visible';
setTimeout(askQuestion1,3000);
}
function showImgBtn2(){
document.getElementById('btn2pic').style.visibility = 'visible';
setTimeout(askQuestion2,3000);
}
//This keeps going for every question--repeated 9 to 20 times


//Questions
function askQuestion1()
{
var answer = prompt ("What body system contains the heart, blood, arteries, and veins?") .toLowerCase();
if (answer==="circulatory") {
alert ("Correct!");
score += 100;
}
else {
alert ("Sorry, incorrect.");
score -= 100;
}
endQuestion();
document.getElementById('btn1pic').style.visibility = 'hidden';
}

function askQuestion2()
{
var answer = prompt ("What body system contains the brain, spinal cord, and nerves?") .toLowerCase();
if (answer==="nervous") {
alert ("Correct!");
score += 200;
}
else {
alert ("Sorry, incorrect.");
score -= 200;
}
endQuestion();
document.getElementById('btn2pic').style.visibility = 'hidden';
}
//This keeps going for every question--same code, just replace the question and answer repeated 9 to 20 times

这是我在 HTML 页面上的调用方式:

<td><button id="btn1" onclick="showImgBtn1();disableButton(btn1)">100</button></td> //each button has a successive ID

这就是我在 HTML 页面上设置图片的方式:

<div>
<img class="picClue" id="btn1pic" src="btn1.jpg" height="200px">
<img class="picClue" id="btn2pic" src="btn2.jpg" height="200px">
<img class="picClue" id="btn3pic" src="btn3.jpg" height="200px">
<img class="picClue" id="btn4pic" src="btn4.jpg" height="200px">
</div>

感谢您的建议!

最佳答案

我发现有一些地方可以改进。您注意到代码中存在大量重复,这是一件好事。首先,函数 askQuestion1askQuestion2 执行完全相同的操作,除了一些变量之外。因此,您可以创建一个函数并将不同的值存储到一个对象中。像这样的事情:

let values = {
question1: {
questionText: "What body system contains the heart, blood, arteries, and veins?",
correctValue: "circulatory",
id: 'btn1pic'
},
question2: {
questionText: "What body system contains the brain, spinal cord, and nerves?",
correctValue: "nervous",
id: 'btn2pic'
}
}

let score = 0;

function askQuestion(question)
{
var answer = prompt(question.questionText);
// validate null and check if the answer is correct.
if (answer && answer.toLowerCase() === question.correctValue) {
alert ("Correct!");
score += 100;
}
else {
alert ("Sorry, incorrect.");
score -= 100;
}
endQuestion();
document.getElementById(question.id).style.visibility = 'hidden';
}

function endQuestion() {}

askQuestion(values.question1);
askQuestion(values.question2);

Edric the comment中指出,最好验证您的用户没有取消提示,从而导致 answer 变量中出现空值。

您甚至可以将问题保存在数组中,并使用 for 循环来询问每个问题:

    let values = [
{
questionText: "What body system contains the heart, blood, arteries, and veins?",
correctValue: "circulatory",
id: 'btn1pic'
},
{
questionText: "What body system contains the brain, spinal cord, and nerves?",
correctValue: "nervous",
id: 'btn2pic'
}
]
/* ... */
for(let i = 0; i < values.length; i ++) {
askQuestion(values[i]);
}

同样的事情也适用于您的 showImgBtn 函数,拥有问题变量会减少重复。

//Toggle Images On
function showImgBtn(question){
document.getElementById(question.id).style.visibility = 'visible';
setTimeout(() => { askQuestion(question); },3000);
}

除此之外,一切都很好。

我强烈建议您也检查评论,很多人都提出了改进我的答案的方法。

关于javascript - 有没有办法简化这段 JavaScript 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59376354/

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