'Cause I’m getting Undefined as a response qd call the function for the second time

Asked

Viewed 40 times

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.

  • 2

    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 keyword const: cannot change the value and keeps comparing always equal. Inspect the values;

  • @eduardoBissi It worked. Thanks

No answers

Browser other questions tagged

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