有没有一种方法可以简化此JavaScript代码?

时间:2019-12-17 14:27:05

标签: javascript arrays function simplify

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

我有一个用于危险游戏的JavaScript系列。我敢肯定,这样做有更好的方法,因为有很多重复。我在想一些带有一些变量,数组和/或在调用函数时传递ID的东西,但不知道从哪里开始。任何想法,将不胜感激。我并不是要任何人去做这项工作,只是给我一些想法和例子,以说明前进的方向。

df=sf.withColumn("message",F.concat_ws(df.data.message))

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

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

谢谢您的建议!

1 个答案:

答案 0 :(得分:5)

我看到一些可以改进的地方。注意到您的代码中有很多重复是一件好事。首先,功能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所指出的,最好验证用户没有取消提示,从而使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);
}

除此之外,它还可以接缝。

我也强烈建议您也查看评论,许多人都在建议改善我的答案的方法。