我是JS的新手。 我必须编写一个包含(循环,条件语句和运算符)的函数...
提出3个问题 - 就我而言
两次尝试正确回答
我只有如下所示。我现在完全被困住了。求救!
function capitalCity()
{
var answer1 = prompt("What is the capital of France?","");
var answer2 = prompt("what is the capital of Scotland?","");
var answer3 = prompt("what is the capital of Spain?","");
if ( answer1 == "Paris" && answer2 == "Edinburgh" && answer3 == "Madrid")
{
alert("You have passed!");
}
else if ( answer1 != "Paris" || answer2 != "Edinburgh" || answer3 != "Madrid")
{
alert("Have another go!");
capitalCity();
}
}
答案 0 :(得分:1)
以下是您问题的另一种解决方案。您可以根据需要使用任意数量的问题:
function askQuestions(secondTime) {
var questionsAndAnswers = [
["What is the capital of France?", "Paris"],
["what is the capital of Scotland?", "Edinburgh"],
["what is the capital of Spain?", "Madrid"]
];
var correctAnswers = 0;
for(var i = 0; i < questionsAndAnswers.length; i++) {
var questionAndAnswer = questionsAndAnswers[i],
answer = prompt(questionAndAnswer[0], "");
if(answer.toLowerCase() === questionAndAnswer[1].toLowerCase()) {
correctAnswers++;
}
}
var ratio = Math.round(correctAnswers/questionsAndAnswers.length*100);
alert(ratio + "% questions are correct");
if(!secondTime && ratio < 100) {
askQuestions(true);
}
}
askQuestions();
答案 1 :(得分:0)
如果您希望仅运行两次,请在循环外部创建一个计数器变量。这就是它的样子。
var count = 0;
function capitalCity()
{
if(count >= 2){
count = 0;
alert('you failed...');
return;
}
var answer1 = prompt("What is the capital of France?","");
var answer2 = prompt("what is the capital of Scotland?","");
var answer3 = prompt("what is the capital of Spain?","");
if ( answer1 == "Paris" && answer2 == "Edinburgh" && answer3 == "Madrid")
{
alert("You have passed!");
count = 0;
}
else if ( answer1 != "Paris" || answer2 != "Edinburgh" || answer3 != "Madrid")
{
alert("Have another go!");
count++;
capitalCity();
}
}
答案 2 :(得分:0)
更新:我错过了两次尝试的标准。
以下是更新的解决方案[demo]:
function capitalCity() {
var countryCapitals = {
France: 'Paris',
Scotland: 'Edinburgh',
Spain: 'Madrid'
};
var correctAnswersCount = 0;
for (var country in countryCapitals) {
var question = 'What is the capital of ' + country + '?',
answer = countryCapitals[country];
if (prompt(question) == answer || prompt('Try again:\n' + question) == answer) {
correctAnswersCount++;
}
}
alert('Number of correct answers: ' + correctAnswersCount);
}
capitalCity();
请注意,使用提示/提醒会产生相当糟糕的用户界面,但是当您在问题中使用提示时,它会使答案变得简单。
答案 3 :(得分:0)
function Trivia(attempts, attempt) {
var questions = [],
correct = 0;
attempt = attempt || 0;
questions.push({
question: 'What is the capital of France?',
answer : 'Paris',
});
questions.push({
question: 'What is the capital of Scotland?',
answer : 'Edinburgh',
});
questions.push({
question: 'What is the capital of Spain?',
answer : 'Madrid',
});
for (var i in questions) {
var answer = prompt(questions[i].question);
if (answer.toLowerCase() == questions[i].answer.toLowerCase()) {
correct++;
}
}
if (correct < questions.length) {
if (attempt >= attempts - 1) {
alert('Sorry, u suck too much');
return;
}
alert('U suck, retry');
Trivia(attempts, attempt+1);
} else {
alert('Good Job!');
}
}
Trivia(3);
//call trivia with 3 max tries