I am working on a project with Vue.JS and Laravel 5.2.
It is a program to train vocabulary. So there is an array in my Vue-Data-Object, containing wordpairs. Each wordpair is stored as an object:
"words": [
{
"lang1": "Haus",
"lang2": "house",
"lang1_hint": "",
"lang2_hint": "",
"image_url": "",
"tries": 0,
"fails": 0,
"process": 1
},
{
"lang1": "Feuer",
"lang2": "fire",
"lang1_hint": "",
"lang2_hint": "",
"image_url": "",
"tries": 0,
"fails": 0,
"process": 5
},
...
]
Now i have a function, which takes the length of the array, generates a random number and returns a random number out of the array above:
getRandomWord: function(){
var i = Math.floor((Math.random() * this.words.length) + 1);
if(this.words[i].process == 5){
return this.getRandomWord();
} else {
return {
index: i,
content: this.words[i]
}
}
}
Mostly there is no problem. But sometimes, there occurs an error saying:
Uncaught TypeError: Cannot read property 'process' of undefined
The console says, that the error occurs in the if-condition of my getRandomWord() if(this.words[i].process == 5).
Any idea why? The value of process is always 1, 2, 3, 4 or 5.
Thanks so far!