Random Quote Generator Not Working

Hi I really new to coding in general. I iave followed instruction but its still doesnt work. PLease help.’’

Look at line 11 in script.js

quote.lenth should be quotes.length

Thanks i fixed that but when i run it, it still doesnt show any quotes?

Quote Gen

Simple Quote Generator

New Quote
                                                  <--- script.java -->

var quotes = [
“tester is a tester”
“Chin Up”
“Churred up bro”
“dancing can be fun”
“lets play pool”
“swiming is fun”
“winning lotto will make everyone happy”
]
function newQuote() {
var randomNumber = Math.floor(Math.random() * (quotes.lenth));
document.getElementById(‘quotes’).innerHTML = quotes[randomNumber]
// body…
}

<---->

Quote Gen

Simple Quote Generator

New Quote <----> var quotes = [ "tester is a tester" "Chin Up" "Churred up bro" "dancing can be fun" "lets play pool" "swiming is fun" "winning lotto will make everyone happy" ] function newQuote() { var randomNumber = Math.floor(Math.random() * (quotes.lenth)); document.getElementById('quotes').innerHTML = quotes[randomNumber] // body... }

I cant post my html code it just changes its self every time

There is another error in your code. Look at line 12 in script.js
The div you want to attach text to has an id of “quoteDisplay” in the html, but the div you are selecting in the function has an id of “quotes”.

Thank you but it still doesnt work. the html loads but the click button doesnt show any quotes

I knocked up an example that works on my machine.

<!DOCTYPE HTML>
<html>
    <head>
        <title>Random Quote Generator</title>
    </head>
    <body>
        <h1>Random Quote Generator</h1>
        <div id="quotedisplay"></div>
        <button onClick="newQuote()">Get New Quote</button>
        <script src="script.js"></script>
    </body>
</html>
var quotes = [
"tester is a tester",
"Chin Up",
"Churred up bro",
"dancing can be fun",
"lets play pool",
"swiming is fun",
"winning lotto will make everyone happy"
];

function newQuote(){
    var randomNumber = Math.floor(Math.random() * (quotes.length));
    document.getElementById("quotedisplay").innerHTML = quotes[randomNumber]
}
1 Like

Hi.
ltwest
The comma is missing from the array elements.

1 Like

Thank You Pethaf, your code wrks