Random Quote Generator - JavaScript code is causing an error. (Help)

Random Quote Generator - JavaScript code is causing an error. (Help)
0

#1

Hello all, it seems my java-script is malfunctioning in some way. I will accept any general feedback/advice to correct the issue.

JS code:
$(document).ready(function(){

function getQuote(){

var quotes = ["You know you're in love when you can't fall asleep because the reality is finally better than your dreams.", "Don't cry because it's over, smile because it happened.", "Don't walk behind me; I may not lead. Don't walk in front of me; I may nor follow. Just walk beside me and be my friend.", "You've gotta dance like there's nobody watching, Love like you'll never be hurt, Sing like there's nobody listening, And live like it's heaven on earth.", "Be who you are and say what you feel, because those who mind don't matter, and those who matter don't mind.", "No one can make you feel inferior without your consent.", "Be yourself; everyone else is already taken.", "I'm selfish, impatient and a little insecure. I make mistakes, I am out of control and at times hard to handle. But if you can't handle me at my worst, then you sure as hell don't deserve me at my best."];
var author = ["-Dr. Seuss", "-Dr. Seuss", "-Albert Camus", "-William W. Purkey", "-Bernard M. Baruch", "-Eleanor Roosevelt", "-Oscar Wilde", "-Marilyn Monroe"];

var randomNum = Math.floor((Math.random()*quotes.length));
var randomQuote = quotes[randomNum];
var randomAuthor = author[randomNum];

$(".quote").text(randomQuote);
$(".author").text(randomAuthor);

$(".btn").on(“click”, function(){
getQuote();
});

});

Source:
https://codepen.io/fields468/


#2

The javascript seems to be working fine:

  var quotes = ["You know you're in love when you can't fall asleep because the reality is finally better than your dreams.", "Don't cry because it's over, smile because it happened.", "Don't walk behind me; I may not lead. Don't walk in front of me; I may nor follow. Just walk beside me and be my friend.", "You've gotta dance like there's nobody watching, Love like you'll never be hurt, Sing like there's nobody listening, And live like it's heaven on earth.", "Be who you are and say what you feel, because those who mind don't matter, and those who matter don't mind.", "No one can make you feel inferior without your consent.", "Be yourself; everyone else is already taken.", "I'm selfish, impatient and a little insecure. I make mistakes, I am out of control and at times hard to handle. But if you can't handle me at my worst, then you sure as hell don't deserve me at my best."];
var author = ["-Dr. Seuss", "-Dr. Seuss", "-Albert Camus", "-William W. Purkey", "-Bernard M. Baruch", "-Eleanor Roosevelt", "-Oscar Wilde", "-Marilyn Monroe"];

var randomNum = Math.floor((Math.random()*quotes.length));
var randomQuote = quotes[randomNum];
var randomAuthor = author[randomNum];

console.log(randomQuote);
console.log(randomAuthor);

I plugged this in to repl.it and that part is working just fine. It seems something else is going on.


#3

Hi

  1. Your codepen does not include jQuery, goto settings -> Javascript -> quick-add (very bottom) -> pick jQuery
  2. Here is your formatted code:
$(document).ready(function() {

            function getQuote() {

                var quotes = ["You know you're in love when you can't fall asleep because the reality is finally better than your dreams.", "Don't cry because it's over, smile because it happened.", "Don't walk behind me; I may not lead. Don't walk in front of me; I may nor follow. Just walk beside me and be my friend.", "You've gotta dance like there's nobody watching, Love like you'll never be hurt, Sing like there's nobody listening, And live like it's heaven on earth.", "Be who you are and say what you feel, because those who mind don't matter, and those who matter don't mind.", "No one can make you feel inferior without your consent.", "Be yourself; everyone else is already taken.", "I'm selfish, impatient and a little insecure. I make mistakes, I am out of control and at times hard to handle. But if you can't handle me at my worst, then you sure as hell don't deserve me at my best."];
                var author = ["-Dr. Seuss", "-Dr. Seuss", "-Albert Camus", "-William W. Purkey", "-Bernard M. Baruch", "-Eleanor Roosevelt", "-Oscar Wilde", "-Marilyn Monroe"];

                var randomNum = Math.floor((Math.random() * quotes.length));
                var randomQuote = quotes[randomNum];
                var randomAuthor = author[randomNum];

                $(".quote").text(randomQuote);
                $(".author").text(randomAuthor);
                $(".btn").on(“click”, function() {
                    getQuote();
                });

            });

Now, this one should show you a simple error - you didn’t close } your getQuote function. You need to close your function, but also make sure you put } in right place!


#4

Thanks this was very helpful! I could use some help getting the button to work now. I appreciate your input!


#5

My last sentence was meant to help you make the button work actually :slight_smile: I was pretty sure you would put the bracket incorrectly. Now. Going back to your JS, take a look at it again and tell yourself this one thing - what is is that your function getQuote does, step by step (not what it should, but what it actually does, by looking at code). I am sure this will help you solve your problem.