`Uncaught Referenceerror: floor is not defined` when trying to use floor and Random to choose an item from an array randomly

Asked

Viewed 167 times

1

I have the following array:

var cartasViradas=[
    'assets/img/1.png',
    'assets/img/2.png',
    'assets/img/3.png',
    'assets/img/4.png',
    'assets/img/5.png',
    'assets/img/6.png',
    'assets/img/7.png',
    'assets/img/8.png',
    'assets/img/9.png',
    'assets/img/10.png'
];

And I would like to know how I can randomly get one of these array values within a variable.

When I try to use the floor and random in an . ts extension file of Ionic 2, I get the following error:

Uncaught ReferenceError: floor is not defined

This is the code that is causing the error:

var selecionado = [];

for (var i = 0; i < 10; i++) { // Escolha aleatoriamente um da matriz cartasViradas
    var randomInd = floor(random(cartasViradas.length));
    var face = cartasViradas[randomInd];
    selecionado.push(face);
    selecionado.push(face);
    cartasViradas.splice(randomInd, 1);
}
  • What is the relationship between a Typescript file and not being able to use floor and random? At first, it would be okay. What happens when you try?

  • The only solutions I’ve ever used were using the library Math.. but it doesn’t mean it’s impossible without her, but because you can’t use her ?

  • Pablo shows the following error when I try "Uncaught Referenceerror: floor is not defined". It is because I am developing an android application with Ionic 2 and when I try to use certain javascript commands in a typescript file it simply does not accept and shows this error that is not set.

  • Enter the code that is giving you this error. The chance is high that it has something wrong.

  • var selected = []; for (var i = 0; i < 10; i++) { // Randomly choose one of the face matrix var randomInd = floor(Random(cards.length)); var face = wildcards[randomInd]; selected.push(face); selected.push(face); cards.splice(randomInd, 1); } This is according to this array of my question

  • Your problem is something else.I’ll edit your question with the new details, but please note that you would have gotten a much easier help if you had gone straight to the root of the problem. As it stands, you asked a question to a XY problem

  • I’m sorry, but from what I understand Typescript does not accept this library so I had requested a method without the floor and the Random, but let’s see if something comes out there and thank you.

  • 2

    have tried Math.floor(x)?

  • 2

    What is this floor? You can’t use Math.floor?

  • Yes I put the Math. now you’re saying that the Random is not set

  • You need Math on both of us. Also, you’re using the wrong path. See my answer.

Show 6 more comments

1 answer

2


floor and random are object methods Math. You need to do Math.floor and Math.random. Besides, you were passing the length as a parameter for the random. It is right to multiply. So change this:

var randomInd = floor(random(cartasViradas.length));

That’s why:

var randomInd = Math.floor(Math.random()*cartasViradas.length);
  • It worked, I just don’t understand why, is that I’m following the steps of this tutorial https://pt.khanacademy.org/computing/computer-programming-programming-games-visualizations/memory-game/a/grid-of-tiles and the way it’s there doesn’t work. bugle.

  • I don’t understand why it’s like that there either.

  • 1

    Probably the author of the post on such link created its own functions to "simplify" the use of Math.* and forgot to add to the examples.

Browser other questions tagged

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