请帮助编写使用Prompt和多个if/then语句的JavaScript测验

c0vxltue  于 2022-12-25  发布在  Java
关注(0)|答案(4)|浏览(171)

所以我只知道基本的HTML和CSS,我可以操纵那里的东西,并找出它,但我不是一个开发人员,我可以从头开始写的东西非常有限。
我正在尝试使用javascript prompt函数和if语句创建一个交互式测验。我已经得到了运行的代码,但有一些事情我需要完成,我不能弄清楚。我已经搜索,并没有运气收到[简单]解决我的问题。
1.如果用户回答不正确,我希望他们它返回到他们的问题重试,直到他们得到正确的。
1.做出多个正确答案的最简单方法是什么(即答案=这个、那个或这些...而不仅仅是“这个”或失败)
1.由于某种原因,我的代码在问题3之后没有执行,无法找出原因。
附言。我知道这可能不是一个很好的做法,只有一个巨大的脚本像这样,但如果可能的话,帮助我与我有什么。提前感谢任何帮助,这里是我的函数:https://gist.github.com/anonymous/dcde24cf97df24fe5ca4

pxy2qtax

pxy2qtax1#

你的问题很宽泛,因为设计一件东西总是有一百万种方法,但我会给予你五分钟的答案。
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);
    }
}

1.老实说,你的方法相当原始,你必须在提示符的文本中写入选项。对于多个答案,你必须强迫用户以某种方式分隔他们的答案(例如,用逗号)。但老实说,我会完全放弃提示/警报方法,并使用HTML元素来实现你的目标(假设这是在Web浏览器中)。
1.似乎其他用户已经解决了您的语法错误。
如果这些概念中有许多对您来说是陌生的,我建议您在Codecademy上学习,因为他们有关于Javascript和HTML的优秀课程。

wgxvkvu9

wgxvkvu92#

在你的问题中,花括号不匹配。请看函数括号结束的地方。它在第三个问题上。
试试看

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?");
    }
}
4ioopgfo

4ioopgfo3#

我喜欢用这个代码,因为它很好,简单.无论如何,这里的代码:

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

1u4esq0p4#

这就是我的答案。虽然晚了很多年,但这就是你如何只使用JavaScript和prompt()和alert()来创建一个测验。这是我正在做的一个作业的答案,我也在寻求帮助,但每个人都说要使用HTML,这是我的最后一站,我自己想办法。非常简单。

let question1 = prompt('Who sings shining bright like a diamond?').toLowerCase()
console.log(question1)

let answer1 = 'rihanna'
let affirm1 = 'You are correct!'
let rebuttal1 = 'You are wrong. It is Rihanna'

if (question1 == answer1) {
    alert(affirm1)
    console.log(affirm1)
} else {
    alert(rebuttal1)
    console.log(rebuttal1)
}

let question2 = prompt('Who sings "Gods Plan" ?').toLowerCase()
console.log(question2)

let answer2 = 'drake'
let affirm2 = 'You got it right!'
let rebuttal2 = 'Nope! The answer is Drake.'

if (question2 == answer2) {
    alert(affirm2)
    console.log(affirm2)
} else {
    alert(rebuttal2)
    console.log(rebuttal2)
}

let question3 = prompt('Who sings "Anaconda" ?').toLowerCase()
console.log(question3)

let answer3 = 'nicki minaj'
let affirm3 = 'Yup! You got it!'
let rebuttal3 = "Wow! You really didn't know it was Nicki Minaj"

if (question3 == answer3) {
    alert(affirm3)
    console.log(affirm3)
} else {
    alert(rebuttal3)
    console.log(rebuttal3)
}

相关问题