How to identify the number that was drawn

Asked

Viewed 43 times

1

I would like to know how to obtain the values of the variables nipeSorteado and faceSorteada to compare with displayed output. The output of row 9 will display the values corresponding to the arrays. And I would like to know the random values that were generated before entering the index of the array.

inserir a descrição da imagem aqui

//arrays que armazenam os nipes e os numeros das cartas
var nipes = ['♥', '♦', '♣', '♠'];
var faces = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'];

//sorteando valores para os nipes e para as cartas
var nipeSorteado = nipes[Math.floor(Math.random() * 4)];
var faceSorteada = faces[Math.floor(Math.random() * faces.length)];
document.write("<h1>" + faceSorteada + nipeSorteado + "</h1>");
  • Add code in non-image text format to make it easier for someone to help you.

  • I already entered the code, vlw by the tip.

1 answer

1

It is simple, just receive in variables the random numbers before passing as index of the array.

Instead of directly receiving random value as array index

 var nipeSorteado = nipes[Math.floor(Math.random() * 4)];

Pass the Math.floor(..) for a variable and then seven as an index:

var numnipe = Math.floor(Math.random() * 4);
...
..

var nipeSorteado = nipes[numnipe];

Getting the code like this:

    //arrays que armazenam os nipes e os numeros das cartas
    var nipes = ['♥', '♦', '♣', '♠'];
    var faces = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'];
    
    //sorteando valores para os nipes e para as cartas
    
    //Número gerado para os nipes
    var numnipe = Math.floor(Math.random() * 4);
    
    //Número gerado para as faces
    var numface = Math.floor(Math.random() * faces.length);
    
    //Valores das variaveis que você desejar obter o valor
    console.log(numnipe, numface);
    
    var nipeSorteado = nipes[numnipe];
    var faceSorteada = faces[numface];
    document.write("<h1>" + faceSorteada + nipeSorteado + "</h1>");
    
    
    console.log(faceSorteada,nipeSorteado);

Browser other questions tagged

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