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];` }Errores en el código
onclick="newQuote()" y no con onclick="function newQuote()"const randomNumber = Math.floor(Math.random() * list.length); y no const randomNumber = Math.floor(Math.random() * list.lenght);quote-generator y en el script era quotes-generator . Deben ser iguales. 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>
const randomNumber = Math.floor(Math.random()*list.lenght);
Debería ser list.length y no list.length.
' dentro del valor de su matriz, necesita un carácter de escape., . 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>