2014-01-20 189 views
0

我是JS的新手。 我必須寫包含函數(循環,條件語句&運營商)是...比較運算符

  1. 詢問3個問題 - 那就是隻要我

  2. 給出兩次試圖正確地回答

  3. 保持共答對
  4. 只顯示總篩選

我只有這麼遠,如下所示。我現在完全卡住了。請幫助!

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

那麼,有什麼問題:只要你想,你可以使用盡可能多的問題?這是否工作?不行?你究竟在幹什麼? –

+0

它不會跟蹤或運行兩次 – mplungjan

+0

您是否期望某人爲您做某件事而不知道自己被卡住了?更加詳細一些。 –

回答

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(); 
    } 
} 
+0

alert(「You have passed!」);返回;而且你不是指'if(count <= 2)' – mplungjan

+0

你是對的。錯字:/我的意思是'count> = 2'。謝謝你的收穫。 – mjkaufer

+0

謝謝。我會首先嚐試這個解決方案,因爲它看起來離我現有的代碼最近。 – Orbital676

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(); 

注意的是,使用提示/警報使一個相當可怕的UI,但它一直答案簡單,因爲你在你的問題中使用它們。

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(); 
+0

不要嚇到小白 – mplungjan

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