1
Pq this function is only getting the right return once?
data() {
return {
remaining: 8,
doorsAmount: 10,
...
openedDoors: [],
};
},
methods: {
openDoors() {
if (this.remaining > 0) {
let number = this.drawNumber();
console.log('n -', number);
this.openedDoors.push(number);
--this.remaining;
console.log('Array', this.openedDoors, '/ Remaining', this.remaining);
}
},
drawNumber() {
const draw = Math.floor(Math.random() * this.doorsAmount);
if (
draw == this.doorRewarded ||
draw == this.selected ||
this.openedDoors.includes(draw)
) {
console.log('Ops');
this.drawNumber();
return;
}
console.log('draw', draw);
return draw;
},
},
}
The function openDoors()
calls the function drawNumber()
q returns the right number, but the second time q this function is called even if it returns the number correctly in the function openDoors()
var number that receives this number is set to Undefined...
Obs: the function openDoors()
is triggered by a click (on a button for ex) and it is called correctly and tb calls the function drawNumber()
, only q gives 2nd x onwards even the function even though I see correctly drawn number on the console the var is set different... for Undefined...
Obs 2: remaining
still greater q 0 so the function is called.
First the piece below where you write 'ops' should change to
return this.drawNumber();
: Since Return is alone it returns nothing after drawing another number. You will also have to see if the if condition inside drawNumber() is always true. I believe the problem may be in the keywordconst
: cannot change the value and keeps comparing always equal. Inspect the values;– Eduardo Bissi
@eduardoBissi It worked. Thanks
– M.F