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
andrandom
? At first, it would be okay. What happens when you try?– Pablo Almeida
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 ?– 13dev
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.
– Lone Tonberry
Enter the code that is giving you this error. The chance is high that it has something wrong.
– Pablo Almeida
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
– Lone Tonberry
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
– Pablo Almeida
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.
– Lone Tonberry
have tried
Math.floor(x)
?– NoobSaibot
What is this
floor
? You can’t useMath.floor
?– Sergio
Yes I put the Math. now you’re saying that the Random is not set
– Lone Tonberry
You need Math on both of us. Also, you’re using the wrong path. See my answer.
– Pablo Almeida