我正在開發一個瑣事程式,在該程式中,用戶會被隨機問一個問題,并且必須輸入答案。該程式會告訴用戶他們是否答對了問題,并在最后提醒他們他們的分數。這是我的代碼:
function askQuestion() {
score = 0
for (let step = 0; step < 5; step ) {
rando = Math.floor(Math.random() * 5) 1;
switch(rando) {
case 1:
var q1 = prompt("Who won the first ever international soccer World Cup? Please write the first letter in capital, and the following ones in lowercase")
if (q1 == "George Washington") {
alert("Correct!")
score = score 1
} else {
alert("Sorry you are incorrect")
}
break;
case 2:
var q2 = prompt("What is a theorem in math for finding a side in a right triangle, knowing 2 others? Please write the both words in capital")
if (q2 == "Pythagorean Theorem") {
alert("Correct!")
score = score 1
} else {
alert("Sorry you are incorrect")
}
break;
case 3:
var q3 = prompt("Who is the first human ever in space? Please write the full name in capital, and the following ones in lowercase")
if (q3 == "Yuri Gagarin") {
alert("Correct!")
score = score 1
} else {
alert("Sorry you are incorrect")
}
break;
case 4:
var q4 = prompt("Who is the first president of the United States? Please write the full name in capital, and the following ones in lowercase")
if (q4 == "George Washington") {
alert("Correct!")
score = score 1
} else {
alert("Sorry you are incorrect")
}
break;
case 5:
var q5 = prompt("In what country were the Olympics invented? Please write the first letter in capital, and the following ones in lowercase")
if (q5 == "Greece") {
alert("Correct!")
score = score 1
} else {
alert("Sorry you are incorrect")
}
break;
case 6:
var q6 = prompt("What is the capital of France? Please capitalize the first letter")
if (q6 == "France") {
alert("Correct!")
score = score 1
} else {
alert("Sorry you are incorrect")
}
break;
case 7:
var q7 = prompt("What is the most purchased video game of all time? Please capitalize the first letter")
if (q7 == "Minecraft") {
alert("Correct!")
score = score 1
} else {
alert("Sorry you are incorrect")
}
break;
case 8:
var q8 = prompt("What is the most watched television brodcast ever? Please write the full name, capitlizing the abbreviation of the organization it is created by, and then the name too.")
if (q8 == "UEFA Euro 2020") {
alert("Correct!")
score = score 1
} else {
alert("Sorry you are incorrect")
}
break;
case 9:
var q9 = prompt("What is the most popular board game in the world? Please capitalize")
if (q9 == "Chess") {
alert("Correct!")
score = score 1
} else {
alert("Sorry you are incorrect")
}
break;
case 10:
var q10 = prompt("What year was the U.S. Declaration of Independence written and ratified?")
if (q10 == "1776") {
alert("Correct!")
score = score 1
} else {
alert("Sorry you are incorrect")
}
break;
default:
alert("This is impossible")
break;
}
}
alert("Thanks for playing! Your score is " score " out of 5!")
}
askQuestion()
我無法找到不讓程式使用陣列兩次提問的方法。有人可以幫助我嗎?謝謝你。
uj5u.com熱心網友回復:
這是一個提示,放棄開關。擁有一組包含所有問題和答案的物件,例如:
const data: [
{ question: "Who?", answer: "Him" },
{ question: "What?", answer: "That" },
{ question: "Where?", answer: "There" }
]
拋棄 for 回圈,使用 while 回圈,并重用邏輯。您只需要隨機索引
while ( data.length > 0 ) {
let randomIndex = Math.floor(Math.random() * data.length) 1;
var userAnswer = prompt(data[randomIndex].question)
if (userAnswer === data[randomIndex].answer) {
alert("Correct!")
score = score 1
} else {
alert("Sorry you are incorrect")
}
data.splice(randomIndex, 1); // remove that question and answer set
}
uj5u.com熱心網友回復:
您可以創建一個包含所有問題及其答案的陣列,如下例所示。
重要提示:在您的askQuestions函式之外定義這些問題,因為您只希望在頁面加載時將它們定義一次。
const questions = [
{
question: 'Who won the first ever international soccer World Cup? Please write the first letter in capital, and the following ones in lowercase',
answer: 'george washington'
},
{
question: 'What is a theorem in math for finding a side in a right triangle, knowing 2 others? Please write the both words in capital',
answer: 'pythagorean theorem'
},
{
...
},
...
];
然后從陣列中隨機選擇一個問題。這與您當前的方法類似。
const randomIndex = Math.floor(Math.random() * questions.length);
const randomQuestion = questions[randomIndex];
從這里您可以訪問問題物件中的question和answer屬性。您可以應用與您已經做過的相同的邏輯來提出問題并檢查答案。
現在,如果答案是正確的,那么您就不想再問那個問題了。questions在給出一個好的答案后,您可以通過從我們的陣列中洗掉問題來做到這一點。
使用陣列.splice上的方法,question我們可以根據索引從陣列中洗掉單個專案。我們已經存盤了索引randomIndex,我們需要它來從陣列中洗掉問題。
在頁面重繪 之前無法再次提問。
const answer = prompt(randomQuestion.question);
if (answer.toLowerCase() === randomQuestion.answer) {
score ;
// Remove 1 item starting from the randomIndex value.
questions.splice(randomIndex, 1);
alert('Correct!');
} else {
alert('Incorrect!')
}
作為補充,我建議您將答案全部定義為小寫,并將用戶的答案也轉換為小寫,然后進行比較。您可以使用.toLowerCase()方法對prompt回傳的字串執行此操作。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/401759.html
標籤:javascript 数组
