Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

193
Views
¿Cómo elimino un elemento aleatorio de una matriz solo UNA VEZ? Sigo obteniendo más de 2 de cada letra en mi matriz

crear una función para mostrar un tablero de juego

 function displayBoard(value)

cree variables para que la función obtenga el valor de la cantidad de tarjetas, cree una instancia de la matriz y obtenga el elemento div por id para poder insertar mis futuros div

 var numOfCards = value * 2; //number of cards that we will have on the board by multiplying input value by 2 var letters2 = ["A", "B", "A", "B"]; // setting up an array of letters to use for the card faces var gameDiv = document.getElementById("game"); //getting the game div element

cree un bucle for que recorra la cantidad de tarjetas, y cree un nuevo elemento div y establezca una identificación única para ese div, además de extraer una letra ALEATORIA de la matriz y almacenarla en el html interno de un elemento div. Solo necesito sacar los elementos una vez, pero no funciona. Traté de hacer estallar el elemento que saqué de la matriz, pero parece ser pasando ese método.

 for (var i = 0; i < numOfCards; i++) { var cards = document.createElement("div"); //creating an div element that is set to variable cards cards.setAttribute("id", "card" + [i]); //setting each card with a unique ID gameDiv.appendChild(cards); //appending the div elements to the "game" div var currentCard = document.getElementById("card" + [i]);//creating a variable for the current card and getting the id to input text var randomItem = letters2[Math.floor(Math.random() * letters2.length)]; //create random item to store random array variable currentCard.innerHTML = randomItem; //setting the current cards html to the random array value letters2.pop(currentCard); //popping the array item so that it can not be used more than once }

¡Gracias por toda la ayuda!

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!