0

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

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

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

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

4

3 回答 3

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. 老实说,使用您的方法非常原始,您必须使用必须在提示文本中编写选项。对于多个答案,您必须强制用户以某种方式分隔他们的答案(例如,使用逗号)。但老实说,我会完全放弃提示/警报方法,并使用 HTML 元素来实现您的目标(假设这是在 Web 浏览器中)。

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

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

于 2013-10-15T03:11:13.340 回答
0

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

尝试

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?");
    }
}
于 2013-10-15T03:11:20.190 回答
0

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

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.")};

于 2018-02-16T22:37:22.600 回答