How to access a varial that is within a function?

Asked

Viewed 37 times

0

I’m trying to access my JSON via a variable that I put in Try and catch , but I can’t access it in other parts of the project, this makes things very difficult because all the project data is provided by this variable. I would like to access it in some way, because when I try to put inside a function it says that the variable is not defined. In this case the variable would be const json = JSON.parse(request.responseText);

I tried to put it global but without success

let randomQuestion;
let round = 0;

function loadJSON() {
  const request = new XMLHttpRequest();
  request.open("get", "./../json/quiz.json");
  request.onload = () => {
    try {
      const json = JSON.parse(request.responseText);
      displayQuestion(json);
    } catch (error) {
      console.warn("Could not load JSON ");
    }
  };
  request.send();
}

function displayQuestion(json) {
  // console.log(json.banco_questoes[1]); 
  randomQuestion = Math.floor(Math.random() * 15);
  randomAnswer = Math.floor(Math.random() * 3);
  // Pergunta
  console.log('Questão de numero: ' + randomQuestion);

  let randomQuestionJSON = json.banco_questoes[randomQuestion].pergunta;
  let firstRandomAnswerJSON = json.banco_questoes[randomQuestion].alternativas[0].descricao;
  let secondRandomAnswerJSON = json.banco_questoes[randomQuestion].alternativas[1].descricao;
  let thirdRandomAnswerJSON = json.banco_questoes[randomQuestion].alternativas[2].descricao;

  // Display the question in DOM
  document.getElementById("paragraph").innerHTML = randomQuestionJSON;

  // Alternativa
  document.getElementById("answer-1").innerHTML = firstRandomAnswerJSON;
  document.getElementById("answer-2").innerHTML = secondRandomAnswerJSON;
  document.getElementById("answer-3").innerHTML = thirdRandomAnswerJSON;
}

document.addEventListener("DOMContentLoaded", () => {
  loadJSON();
});

document.getElementById('btn-confirm').addEventListener('click', function() {
  // somar o round
  round++;
  console.log(round);
  // verificar quando acaba
  if (round === 15) {
    console.log('Game is Over');

    // termina o jogo
  }
  // fazer a verificatáo da resposta correta

  // if resposta correta soma e atualiza a HUD 
});

function test(json) {

  // console.log('work bitch');
  console.log(json.banco_questoes[randomQuestion].alternativas[randomAnswer].impacto_indicadores.satisfacao);
  console.log(json.banco_questoes[randomQuestion].alternativas[randomAnswer].impacto_indicadores.fidelizacao);
  // console.log(json.banco_questoes[firstRandomAnswerJSON].alternativas[firstRandomAnswerJSON].impacto_indicadores.satisfacao);
  // console.log(json.banco_questoes[firstRandomAnswerJSON].alternativas[firstRandomAnswerJSON].impacto_indicadores.satisfacao);
  // console.log(json.banco_questoes[firstRandomAnswerJSON].alternativas[firstRandomAnswerJSON].impacto_indicadores.fidelizacao);
}

  • 3

    You would have to declare the variable with global scope outside the function with let json; and in AJAX switch to json = JSON.parse(request.responseText);.

  • You’re using the right way, you really have to chain things up from that XHR. What quiz.json? so I can set an example,

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.