【问题标题】:Please help writing a JavaScript quiz using Prompt and multiple if/then statements请帮助使用 Prompt 和多个 if/then 语句编写 JavaScript 测验
【发布时间】:2013-10-15 02:49:15
【问题描述】:

所以我只知道你的基本 HTML 和 CSS。我可以操纵那里的东西并弄清楚它,但我不是开发人员,我能从头开始写的东西非常有限。

我正在尝试使用 javascript 提示函数和 if 语句创建交互式测验。我已经运行了代码,但是我需要做一些我无法弄清楚的事情。我进行了搜索,但没有收到针对我的问题的 [简单] 解决方案。

  1. 如果用户回答不正确,我希望他们返回问题再试一次,直到他们答对为止。
  2. 做出多个正确答案的最简单方法是什么(即 answer = this、that 或 these...不仅仅是“this”或失败)
  3. 由于某种原因,我的代码在问题 3 之后没有执行,不知道为什么。

PS。我知道只有一个像这样的大型脚本可能不是一个好习惯,但如果可能的话,请帮助我完成我所拥有的。在此先感谢您的帮助,这是我的功能: https://gist.github.com/anonymous/dcde24cf97df24fe5ca4

【问题讨论】:

  • 你也把答案放在 JS 里?我想这是一款非常休闲的问答游戏。
  • 花括号 { 在函数中不匹配
  • 就像我说的那样,你可以得到尽可能基本的东西。如果用户回答错误,我不知道如何让用户返回问题,而且我无法弄清楚为什么在问题 3 之后它没有执行。
  • 有问题的函数完成了...见//Question 4 }函数括号在这里完成
  • 谢谢泰米尔语,我找到了。更新了我的代码。如果用户答错了,如何让用户返回问题?

标签: javascript if-statement prompt


【解决方案1】:

您的问题非常广泛,因为设计某物的方法总是有上百万种,但我会给您五分钟的答案。

  1. 使用数据结构来保存您的问题/答案组合,例如对象数组。如果您想让用户沿着“路径”前进,那么数组是有意义的。您只需遍历数组即可提出问题。

    var questions = [{ 
            question: 'Are you happy?',
            answer: 'yes',
            affirm: 'Yay! You got it right!',
            rebuttal: 'Nope, you are definitely happy.'
        },
        {
            question: 'Are you mad?',
            answer: 'no',
            affirm: 'Good job!',
            rebuttal: 'Not right.'
        }];
    
    for (var i = 0, l = questions.length; i < l; i++) {
        answer = prompt(questions[i].question);
    
        // I do not support manipulating a loop counter mid-loop, but it's for an example.
        if (answer !== questions[i].answer) {
            alert(questions[i].rebuttal);
            i--;
        } else {
            alert(questions[i].affirm);
        }
    }
    
  2. 说实话,你的方法非常原始,你必须在提示文本中写下选择。对于多个答案,您必须强制用户以某种方式分隔他们的答案(例如,使用逗号)。但老实说,我会完全放弃 prompt/alert 方法,并使用 HTML 元素来实现您的目标(假设这是在 Web 浏览器中)。

  3. 似乎其他用户已经解决了您的语法错误。

如果您对这些概念中的许多概念感到陌生,我建议您在 Codecademy 学习,因为他们提供有关 Javascript 和 HTML 的优秀课程。

【讨论】:

  • 谢谢马克!这很有帮助。
  • 现在似乎一切正常。但是,由于某种原因,它正在打破第一次反驳。如果回答不正确,只会将您踢出 js。如果您前进并错误地回答了问题 2,它会从问题 1 中拉出第一个反驳……这是一个落后的反驳。 gist.github.com/anonymous/09348cc3994fd34d0d24
【解决方案2】:

在您的问题中,大括号不匹配。查看函数大括号完成的位置。这是第三个问题。

试试

function welcome() {
    confirm("Welcome! You have chosen to play. You will be presented with a series of questions...");
    confirm("If you answer a questions incorrectly, you cannot advance to the next...");
    var retVal = prompt("Do you want to continue?");
    if (retVal == "yes") {
        alert("Good, question 1...");
    } else {
        alert("Well you're boring");
    }
    //Question 1
    var retVal = prompt("The term jitterbug originally was a slang term for a person who was real heavy on jittersauce. What does jittersauce refer to?");
    if (retVal == "alcohol") {
        alert("Not bad, next question...");
    } else {
        alert("FALSE. Please try again. HINT: it makes parties fun...");
    }
    //Question 2
    var retVal = prompt("What early American Broadway show helped popularize swing dance styles, ranging from the Charleston to the Foxtrot?");
    if (retVal == "Ziegfeld Follies") {
        alert("Dang, pretty impressive. NEXT");
    } else {
        alert("NOPE. HINT: starts with a Z...");
    }
    //Question 3

    var retVal = prompt("What ballroom style is inspired by the traditional Spanish bullfight?");
    if (retVal == "paso doble") {
        alert("Very good... your Googling skills are excellent.");
    } else {
        alert("WRONG. HINT: it's in Spanish...");
    }
    //Question 4

    var retVal = prompt("Ballroom dancing gets its name from what Latin term?");
    if (retVal == "ballare") {
        alert("More like BALLER... HAH, ok next.");
    } else {
        alert("Really? Come on. HINT: it's got the word ball in it...");
    }
    //Question 5
    var retVal = prompt("What Latin dance style is the official dance of the Dominican Republic?");
    if (retVal == "Merengue") {
        alert("Tastes like lemons, time for the final question");
    } else {
        alert("INCORRECT. HINT: starts with an M and rhymes with a fruit pie...");
    }
    //Question 6
    var retVal = prompt("What Latin dance style is a combination of the Mambo and the Rumba?");
    if (retVal == "cha cha") {
        alert("YOU WIN YOU WIN YOU WIN... click OK to claim your prize");
    } else {
        alert("DUR NO. HINT: this is a text message service that will answer any question you text them...");
    }
    //Prize

    var retVal = prompt("This is your prize. Will you accept?");
    if (retVal == "yes") {
        alert("Good. Comence prizing.");
    } else {
        alert("Why you no want prize?");
    }
}

【讨论】:

    【解决方案3】:

    我喜欢使用这段代码,因为它既好又简单。无论如何,这是代码:

    var q; function w(e,r){while(q!=r){q=prompt(e);};}; w("question", "answer");

    如果你只是添加;,然后在末尾添加w("whatever your question is", "whatever your answer is"),它会添加另一个问题。但不要忘记问题和答案之间的逗号。

    但是如果你想选择你想要它做什么,那么你使用这个:

    var x=prompt("what is 1+1?"); if(x=="2"){alert("yep!")} else{alert("no.")};

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多