Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

241
Visualizações
Lo he intentado de varias maneras, ¿alguien puede decirme qué le pasa a esta matriz?

Estoy tratando de hacer un generador de cotizaciones aleatorias que genere una cotización con solo hacer clic en un botón. Creo que he hecho todo bien, pero una vez que se hace clic en el botón, no sucede nada.

Aquí está el código

Código HTML

 <div class="quote-box"> <p id = "quote-generator"> this is where the quote will go </p> <button class="btn" onclick="function newQuote()"> Next </button> </div>

codigo js

 var list = [ '/"Your mind will always believe what you tell it. Feed it faith. Feed it the truth. Feed it with love. /"', '/"A problem is a chance for you to do your best./"', '/"Learn how to be happy with what you have while you pursue all that you want./"',];` const randomNumber = Math.floor(Math.random()*list.lenght); function newQuote() { document.getElementById("quotes-generator").innerHTML = list [randomNumber];` }
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Errores en el código

  • La llamada a la función debe hacerse al hacer clic con onclick="newQuote()" y no con onclick="function newQuote()"
  • Error tipográfico al tomar la longitud de la lista para generar un número aleatorio. Debe ser const randomNumber = Math.floor(Math.random() * list.length); y no const randomNumber = Math.floor(Math.random() * list.lenght);
  • El ID especificado en HTML era quote-generator y en el script era quotes-generator . Deben ser iguales.
  • También puede mover la lógica de generación de números aleatorios dentro de la función para generar un nuevo número aleatorio cada vez que el usuario hace clic en el botón. La generación de números aleatorios actual ocurre solo una vez y el valor no se actualizará cuando el usuario haga clic en el botón

 var list = [ '/"Your mind will always believe what you tell it. Feed it faith. Feed it the truth. Feed it with love. /"', '/"A problem is a chance for you to do your best./"', '/"Learn how to be happy with what you have while you pursue all that you want./"', ]; function newQuote() { const randomNumber = Math.floor(Math.random() * list.length); document.getElementById("quote-generator").innerHTML = list[randomNumber]; }
 <div class="quote-box"> <p id="quote-generator"> this is where the quote will go </p> <button class="btn" onclick="newQuote()"> Next </button> </div>

La representación mínima de su solución será

 const list = [ '"Your mind will always believe what you tell it. Feed it faith. Feed it the truth. Feed it with love. "', '"A problem is a chance for you to do your best."', '"Learn how to be happy with what you have while you pursue all that you want."', ]; newQuote = () => document.getElementById("quote-generator").innerHTML = list[Math.floor(Math.random() * list.length)];
 <div class="quote-box"> <p id="quote-generator"> this is where the quote will go </p> <button class="btn" onclick="newQuote()"> Next</button> </div>

about 4 years ago · Juan Pablo Isaza Relatório

0

const randomNumber = Math.floor(Math.random()*list.lenght);

Debería ser list.length y no list.length.

about 4 years ago · Juan Pablo Isaza Relatório

0

  1. En su ejemplo, no es necesario escapar del carácter en la matriz. Si la comilla simple ' dentro del valor de su matriz, necesita un carácter de escape.
  2. Fin de la matriz que tiene extra , .
  3. Manera incorrecta de llamar a la función al hacer clic en el botón.

 var list = ['"Your mind will always believe what you tell it. Feed it faith. Feed it the truth. Feed it with love. "', '"A problem is a chance for you to do your best."', '"Learn how to be happy with what you have while you pursue all that you want."'];; function newQuote() { let rnd = Math.floor(Math.random() * list.length); let rqt = list[rnd]; document.getElementById("quote-generator").innerHTML = rqt; }
 <div class="quote-box"> <p id="quote-generator"> this is where the quote will go </p> <button class="btn" onclick="newQuote()"> Next </button> </div>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda