Alert with Fisher – Yates shuffle / Market App

I’ve a list of market items, every time the customer confirms the alert, the list must be mixed up. I’m using Fisher - Yates shuffle, but when I look at the “serve --lab” result, the shuffling doesn’t happen.

public Vegetables = [‘Carrot.’, ‘Broccoli.’, ‘Asparagus.’, ‘Cauliflower.’, ‘Corn.’, ‘Cucumber.’, ‘Eggplant.’, ‘Lettuce.’, ‘Radish.’, ‘Okra.’]

async Vegatableslist() {

const alert = await this.alert.create({

  cssClass: 'my-custom-class',

  message: 'Do you want to reorder items?',

  buttons: [

    {

        text: 'No.',

        handler: () => {

        }

    },

    {

       text: 'Yes, please.',

       handler: () => {

function shuffle(array) {

var Vegetables = array.length, t, i;

while (Vegetables) {

i = Math.floor(Math.random() * Vegetables- -);

t = array[Vegetables];

array[Vegetables] = array[i];

array[i] = t;

}

return array;

}

       }

   }

],

});

await alert.present();

}

I used this code: Fisher–Yates Shuffle. My Ts does not report an error, but the action does not happen in “serve --lab” and this message appears on function shuffle: " ‘shuffle’ is declared but its value is never read."

Can someone help me? Alert and scrambling are company criteria.

You are attempting to sleep in a picture of a hotel room.

The definition of a function is not the same thing as invoking it. The error message you see is trying to tell you this, but I can see how it wouldn’t make much sense unless you understand the underlying problem.

$ node
> function afo() { console.log("afo"); }
undefined

That defines a function. It is key to note that the result of this definition is undefined.

Let’s call it.

> afo();
afo
undefined

It logged “afo” and then again we see an undefined. That’s because afo() doesn’t return anything, so from the JS engine’s point of view, it returns undefined. Let’s do the same thing with arrow functions:

> let bfo = () => { console.log("bfo"); };
undefined
> bfo();
bfo
undefined

Looks very similar and works the same way. There are some differences involving variable scoping, but we don’t really care about that here.

Now let’s do what you’re doing:

> let cfo = () => { function dfo() { console.log("dfo"); } };
undefined

cfo is an arrow function that, when called, defines another function dfo that never gets called by anybody anywhere, as demonstrated by:

> cfo();
undefined

There are a lot of ways to untangle this, and cases to be made for many of them. Personally, I would try to get the body of the shuffle out into a separate function somewhere, and make your handler look something like:

handler: () => shuffle(this.vegetables)
1 Like

Worked, thanks! I have 3 more arrays: Meats, Drinks and Cereals.
How do I include shuffling in the same handler? Is possible? All I found was about different handlers in different alerts, that way, my page would be very long.
I tried it in different ways (with different functions) but it didn’t work.