Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

131
Vistas
Javascript: tratando de hacer que el texto se genere aleatoriamente

Estoy tratando de hacer que el texto inicial diga Hi there, as you can see I am Sheldon Aldridge y en la matriz de sentenceArray de javascript contendrá la lista de texto que reemplazará la speech-bubble class con el HTML interno, comencé un bucle for para completar el ciclo a través de la matriz, no estoy seguro de cómo lograr eso?

Resultado: sentenceArray debe reemplazar la speech-bubble al azar con un intervalo de tiempo establecido entre cada texto aleatorio.

HTML

 <div class = "speech-bubble"> <p>Hi there, as you can see I am Sheldon Aldridge</p> </div> </div>

JS

 let sentenceArray = [ "Fun fact about me, I love video games, I have been playing video games since I was 10 years old", "I want to be a front end developer because creating and the building is a passion of mine", "You should really look at the projects I built, you can get a good look at my skillsets", ] function textgen(sentenceArray){ let speechbubble = document.querySelector(".speech-bubble"); let speechlength = speechbubble.length; while(speechbubble){ for(let i = 0; i < speechlength; i++){ } } }
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Aquí hay un pequeño código que hace lo que quiere PERO tiene un problema. ¡Necesita encontrar una manera de generar un número aleatorio pero sin repetir el mismo que acaba de usar! ¡Pero creo que este es un buen punto de partida para ti!

 const sentenceArray = [ "Fun fact about me, I love video games, I have been playing video games since I was 10 years old", "I want to be a front end developer because creating and the building is a passion of mine", "You should really look at the projects I built, you can get a good look at my skillsets", ]; function textgen(){ let random = Math.floor(Math.random() * sentenceArray.length );//this generates a random number between 0 and sentenceArray's length document.querySelector(".speech-bubble").innerHTML= '<p>' + sentenceArray[random] + '</p>' ; setTimeout(()=>{ textgen(); },2000);//2000 means 2secs } textgen();
 <div class = "speech-bubble"> </div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

Esto debería funcionar. usando setInterval con un intervalo de 2 segundos

 let sentenceArray = [ "Fun fact about me, I love video games, I have been playing video games since I was 10 years old", "I want to be a front end developer because creating and the building is a passion of mine", "You should really look at the projects I built, you can get a good look at my skillsets" ]; const el = document.querySelector(".speech-bubble p"); setInterval(() => { const random = Math.floor(Math.random() * sentenceArray.length); el.innerText = sentenceArray[random]; }, 2000);
 <!DOCTYPE html> <html> <head> <title>Parcel Sandbox</title> <meta charset="UTF-8" /> </head> <body> <div class = "speech-bubble"> <p>Hi there, as you can see I am Sheldon Aldridge</p> </div> </div> </body> </html>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda