Switching to the new function writing format

I moved this up in your code and it worked, It’s not working because you are trying to call generateQuestion(currentQuestion); before defining it.

After a function expression has been stored in a variable, the variable can be used as a function. Functions stored in variables do not need function names. The function’s name doesn’t get hoisted if it is part of a function expression.

var pitanje = document.getElementById('question');
var restart = document.getElementById('reset');
var currentQuestion = 0;
var score = 0;

var questions = [
	{
		question: "Best internet service provider is:",
    answers: ["DSLON WIRELESS","BH Telecom","Gigaherc","TELESKY"],
    answer: "DSLON WIRELESS"
	},
  {
		question: "Bad internet service provider is:",
    answers: ["BH Telecom","TELESKY","Gigaherc","DSLON WIRELESS"],
    answer: "Gigaherc"
	},
  {
		question: "What is the name of the President of Russia?",
    answers: ["Donald Trump","Vladimir Vladimirovich Putin","Barack Obama","Bakir Izetbegovic"],
    answer: "Vladimir Vladimirovich Putin"
	}
];

const nextQuestion = ()=> {
  currentQuestion++;
  score ++;
  generateQuestion(currentQuestion);
}
const wrongAnswer = ()=> {
	alert('Odgovor nije tacan,igra krece ponovno!');
  resetGame();
}

var generateQuestion = (id_x) =>  {

  pitanje.innerHTML = '';
  
	if (id_x === questions.length) {
   pitanje.innerHTML = "Vas rezultat je:" + score;
  	return;
  }
	var pitanja = questions[id_x];
	pitanje.innerHTML = '<b>' + pitanja.question + '</b>';  
  var correctAnswer = pitanja.answer;
  for (var i = 0; i < pitanja.answers.length; i++) {
  	var answer = pitanja.answers[i];
  	var odgovaranje = document.createElement('LI');
    odgovaranje.innerHTML = answer;
    odgovaranje.style.cursor="pointer";    
    if (answer === correctAnswer) {
    	odgovaranje.addEventListener('click', nextQuestion);
    } else {
    	odgovaranje.addEventListener('click', wrongAnswer);
    }
    
    pitanje.appendChild(odgovaranje);
  }
}



resetGame =  () => {
	currentQuestion = 0;
  score = 0;
  generateQuestion(currentQuestion);
  alert("igra je ponovno krenula");
}

generateQuestion(currentQuestion);
restart.addEventListener('click', resetGame);
<div id="question"></div>
<button id="reset">Reset game</button>

Leave a Comment