Javascript Function won't work as suppose to do after a few calls

I'm trying to build this app which will generate a random question from two arrays of data: country and questions. So I want to query that question to the HTML and then the user answer the question through a text input and then check if the answer is correct. After a few calls the app works fine, but sometimes won't work as suppose... For example: for the question "Which is the capital of Romania?" the correct answer should be "bucuresti" but for a reason I don't understand the correct answer to pass the question is actually "romania". I'm guessing ther is a problem whit my if/else statement in the chechAnswer()function

Array.prototype.random = function (length) {
  return this[Math.floor(Math.random()*length)];
};

var country = [
      {name:"romaniei", capital: "bucuresti"},
      {name: "bulgariei", capital: "sofia"}
    ],
    questions = [
      "Care este capitala ",
      " este capitala carei tari?"
    ];

document.querySelector('input').onclick= function() {
  var q,
      chosen_country = country.random(country.length),
      chosen_question = questions.random(questions.length);
      
    cc = chosen_country;
    cq = chosen_question;
  if(chosen_question == questions[0]){
    q = chosen_question + chosen_country.name + "?";
  } else if(chosen_question == questions[1]){
    q =  chosen_country.capital + chosen_question;
  }
  
  document.querySelector('#que').innerHTML= q;
  document.getElementById("btn").disabled = true; 

};
 checkAnswer = function() {
     var intrebare;
  var answer = document.myform.ans.value;         
     if(cq == questions[0] && answer == cc.capital){
         alert("corect");
         document.getElementById("btn").disabled = false; 
     }else if (cq == questions[1] && answer == cc.name) {
         alert("corect");
         document.getElementById("btn").disabled = false; 
     }else {
         alert("incorect");
     }
};
<form name="myform">
  <input type="button" id="btn" value="Generate question">
  <div id="que">Intrebare:</div>
   <input type="text" id="ans">
  <input type="button" value="ok" onclick="checkAnswer()"/>
</form>

Upvotes: 1

Views: 65

Answers (2)

JaeGeeTee
JaeGeeTee

Reputation: 523

The if statement in your checkAnswer() function is a little off.

It needs to be:

if(answer === cc.name || answer === cc.capital)

Your current code will not give you the expected results.

Also correct and incorrect are spelled wrong.

Upvotes: 0

Jamiec
Jamiec

Reputation: 136174

This is an invalid boolean check (or, rather, it wont do what you're expecting it to do!):

if(answer === (cc.name || cc.capital)){

That should be

if(answer === cc.name || answer === cc.capital){

Notwithstanding the above, your logic is still a bit off, when choosing a question, you determine whether you want to use the contry or the city as part of the question:

cc = chosen_country;
  if(chosen_question == questions[0]){
    q= chosen_question + chosen_country.name + "?";
  } else if(chosen_question == questions[1]){
    q=  chosen_country.capital + chosen_question;
  }

(Asside: The above code makes picking a random question a little obsolete)

However, when testing the answer, there is no such check, which means the question "What is the capital of romaniei" will accept an answer "romaniei" or "bucuresti".

Upvotes: 3

Related Questions