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

141
Views
cómo hacer que js muestre una palabra aleatoria de una lista con un clic de un botón e imprimirla en un encabezado 3

¿Cómo hacer que js muestre una palabra aleatoria de una lista con un clic de un botón e imprimirla en un encabezado 3? Necesito una respuesta rápida. Por favor, agradecería una respuesta rápida.

// Comienzo del código Js

 let display = document.getElementById("motivato"); console.log(display) var motivation = [{ quote: "Planting popcorn does not produce more popcorn", person: "Farmer Ted" }, { quote: "White whale, bad whale", person: "Confucious (Moby Dick)" }, { quote: "Use the strobe function to disorientate your attacker", person: "Flashlight" }, { quote: "Apply liberally to your erogenous zones", person: "Spice Bomb" }, { quote: "Help me, I'm bleaching", person: "The Great Barrier Reef" }]; function motivateMe() { var listLength = Object.keys(motivation).length; var randVal = Math.floor(Math.random() * listLength); document.write(motivation[randVal]); display.innerHTML = motivation; }

Edite mi código y responda a continuación.

Gracias

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Voy a suponer que querías decir que querías generar una cita aleatoria y no solo una palabra.

Aquí está la respuesta:

 const motivation = [{ quote: "Planting popcorn does not produce more popcorn", person: "Farmer Ted" }, { quote: "White whale, bad whale", person: "Confucious (Moby Dick)" }, { quote: "Use the strobe function to disorientate your attacker", person: "Flashlight" }, { quote: "Apply liberally to your erogenous zones", person: "Spice Bomb" }, { quote: "Help me, I'm bleaching", person: "The Great Barrier Reef" }]; const randomNumber = Math.floor(Math.random() * motivation.length); console.log(motivation[randomNumber]); const generate_quote = () => { const quote = motivation[randomNumber].quote; document.getElementById("random_quote").innerHTML = quote; }
 <p id="random_quote">The random quote will appear here.</p> <button onclick="generate_quote()">Generate random quote</button>

about 4 years ago · Juan Pablo Isaza Report

0

 const display = document.getElementById("motivato"); const motivation = [{ quote: "Planting popcorn does not produce more popcorn", person: "Farmer Ted" }, { quote: "White whale, bad whale", person: "Confucious (Moby Dick)" }, { quote: "Use the strobe function to disorientate your attacker", person: "Flashlight" }, { quote: "Apply liberally to your erogenous zones", person: "Spice Bomb" }, { quote: "Help me, I'm bleaching", person: "The Great Barrier Reef" }]; function motivateMe() { const listLength = motivation.length; const randVal = motivation[Math.floor(Math.random() * listLength)]; display.innerHTML = `<q>${randVal.quote}</q><br><br><small>${randVal.person}</small>`; }
 <button onclick="motivateMe()">Motivate me!</button> <h3 id="motivato"></h3>

Ahora debe editar lo anterior para asegurarse de que al presionar un botón no se devuelva la misma cotización que la anterior, ya que cuando lo hace, parece que el botón no funciona.

about 4 years ago · Juan Pablo Isaza Report
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!