2

I'm learning to code Javascript. I'm trying to create a random quote generator. The idea is that I have a method that creates a multidimensional array, with each element array having a quote and the author's name. This method returns the multidimensional array.

I assign this returned multidimensional array to a variable and pick a random element array. It gives me an "Unexpected Type Error" in the console.

    <script>
   var colors = ['#16a085', '#27ae60', '#2c3e50', '#f39c12', '#e74c3c', '#9b59b6',
                  '#FB6964', '#342224', '#472E32', '#BDBB99', '#77B1A9', '#73A857'];

   console.log("Hi!");

   function getQuote(){
      var quotesAndAuthors =
      [
         ['But suicides have a special language. Like carpenters, they want to know "which tools", they never ask "why build"', "Anne Sexton"],
         ['Here is the world. Beautiful and terrible things will happen. Dont be afraid', 'Frederick Buechner'],
         ['All grown-ups were once children, but only a few of them remember it', 'Alexander de Saint-Exupery'],
         ['It was love at first sight, at last sight, at ever ever sight', 'Vladimir Nabokov'],
         ['A paradise the color of hell flames, but a paradise nonetheless', 'Vladimir Nabokov'],
         ['There is nothing like the deep breaths after laughing that hard. Nothing in the world like a sore stomach for the right reasons','Stephen Chbosky'],
         ['And then suddenly, something is over','Louise Gluck'],
         ['Adventure is out there!', 'Up (Pixar)'],
         ['The strong do what they can, and the weak suffer what the must', 'Thucydides'],
         ['But who prays for Satan? Who, in eighteen centuries, has had the common humanity to pray for the one sinner that needed it most?', 'Mark Twain'],
         ['I stopped explaining myself when I realized people only understand from their level of perception', 'Unknown'],
         ['Unexpressed emotions will never die. They are buried alive and will come forth in uglier ways', 'Sigmund Freud'],
         ['Genius might be the ability to say a profound thing in a simple way', 'Charles Bukowski']
      ];
      return quotesAndAuthors;
   }

   function generate(){
      var pickColor = Math.floor(Math.random * colors.length);

      $('html body').animate({
         backgroundColor: colors[pickColor]
      }, 1000);

      $('#text #author').animate({
         color: colors[pickColor]
      }, 500);

      $('.buttons').animate({
         backgroundColor: colors[pickColor]
      },500);



      var quotes = getQuote();
      var n = Math.floor(Math.random * quotes.length);
      var quoteText = quotes[n][0];
      var quoteAuthor = quotes[n][1];

      $('#text').text(quoteText);
      $('#author').text(quoteAuthor);
   }

   $(document).ready(function(){
      generate();
      alert("Testing");
      $('#quoteButton').on('click', generate());
   });

</script>

Also, suggestions on how to store quotes more effectively would be appreciated.

3 Answers 3

1

random is a function and not a property. You should use paranthesis like

var n = Math.floor(Math.random() * quotes.length);

Also while adding event listeners, you should not use paranthesis, as this will call the method before even click event. Just give the function name.

$('#quoteButton').on('click', generate);

Also it's better to use arrary of objects in your case as below:

var quotesAndAuthors = [
      {
        "quote" : "But suicides have a special language",
        "athour" : "Anne Sexton"
      },
      {
        "quote" : "All grown-ups were once children",
        "athour" : "Alexander de Saint-Exupery"
      }

      ];

And you can access the quote as below using either of the method:

console.log(quotesAndAuthors[0]["quote"]);
console.log(quotesAndAuthors[0].quote);
Sign up to request clarification or add additional context in comments.

1 Comment

"random is a function and not a property." - Isn't it a property that refers to a function? Aren't all object methods basically just properties that refer to functions?
1

.random is not a property of the Math object. Math.random() is a method.

Comments

1

You wanted to call Math.random() because that's a function (note the parenthesis). That was generating your error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.