So I had a variable defined like this:
const myQuestions = [
{
question: "Question 1?",
answers: {
a: "A",
b: "B",
c: "The Correct One"
},
correctAnswer: "c"
},
{
question: "Question 2?",
answers: {
a: "A",
b: "B",
c: "The Correct One"
},
correctAnswer: "c"
}
];
but then I wanted to extract what it's inside the variable from a json file from an URL and put it inside of it, so I deleted that snippet of code and added this one:
var myQuestions;
fetch('someURLtoaJSONfile.json')
.then(response => response.json())
.then(data => {
let jsonString = JSON.stringify(data);
myQuestions = JSON.parse(jsonString);
});
and it tells me that myQuestions
is undefined. However if I define myQuestions = [];
it works but when I console.log(myQuestions
inside the function, it works and it tells me exactly how it should be, but when I use console.log(myQuestions);
outside the function it tells me that the array is empty... why?