I am trying to make a random quote generator that generates a quote by the click of a button. I think I have done everything right, but once the button is clicked nothing happens.
Here is the code
HTML code
<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>
JS code
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];`
}
Mistakes in code
onclick="newQuote()" and not with onclick="function newQuote()"const randomNumber = Math.floor(Math.random() * list.length); and not const randomNumber = Math.floor(Math.random() * list.lenght);quote-generator and in script was quotes-generator. They must be same.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>
The minimal representation of your solution will be
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);
It should be list.length and not list.lenght.
' inside in your array value you need escape character., .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>