Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Trying to grab a quote

I'm a novice coder...I'm trying to create a random number which would be used to grab both a quote and author from variable and have it written. Would love tips and help. Thank you!! (Obviously not finished but would love to know if I'm heading in the right direction).

var quote = [
       "While a people preserves its language; it preserves the marks of liberty ",
        "My mission in life is not merely to survive, but to thrive; and to do so with some passion, some compassion, some humor, and some style. ",
       "Work hard for what you want because it won't come to you without a fight. You have to be strong and courageous and know that you can do anything you put your mind to. If somebody puts you down or criticizes you, just keep on believing in yourself and turn it into something positive. ",
       "Success is no accident. It is hard work, perseverance, learning, studying, sacrifice and most of all, love of what you are doing or learning to do. ",
       "Your work is going to fill a large part of your life, and the only way to be truly satisfied is to do what you believe is great work. And the only way to do great work is to love what you do. If you haven't found it yet, keep looking. Don't settle. As with all matters of the heart, you'll know when you find it. "
];



var author = ["-Jose Rizal",
    "-Maya Angelou",
    "-Leah LaBelle",
    "Pele",
    "Steve Jobs"
    ];

function getRanNum(Math.floor(Math.random() * 5)) {
return getRanNum;
document.write(quote[getRanNum]);
document.write(author[getRanNum]);
}

2 Answers

You could use Math.random and do a count of each array.

, then choose a random number and use that to grab an array index.

However, since you have quotes and related authors, it would be better for you to create a JavaScript Object, then perhaps have an array of those quote objects.

Then when you do a random number in your array, it will return you a Quote object. You could then show something like "quotes[i].quote by quotes[i].author"

Thank you for the tips. Just curious though. Why is it that my current code won't run (sorry and thanks I'm a complete beginner)

There are some issues in your code:

function getRanNum(Math.floor(Math.random() * 5)) {  // you are using an expression in place of a parameter
return getRanNum; // the function returns itself
document.write(quote[getRanNum]); // you aren't calling the function
document.write(author[getRanNum]);
}

First try to assign the random number to a variable, and then return that variable

function getRanNum() {
const number = //write your random number code here
return number;
}

Thank you!

Hi McKay,

To make your logic more flexible, I would count up the quotes.

function getRanNum() {
  var count= quote.length; // So the number is 5
  return Math.floor(Math.random() * count);
}

The better way to do this however is to make a Object Constructor

function Quotation(quote, author) {
  this.quote = quote;
  this.author = author;
}

var arrayOfQuotations = [
  new Quotation("While a people preserves its language; it preserves the marks of liberty ", "Jose Rizal"),
  new Quotation( "My mission in life is not merely to survive, but to thrive; and to do so with some passion, some compassion, some humor, and some style.", "Maya Angelou"),
  new Quotation( "Work hard for what you want because it won't come to you without a fight. You have to be strong and courageous and know that you can do anything you put your mind to. If somebody puts you down or criticizes you, just keep on believing in yourself and turn it into something positive.", "Leah LaBelle"),
  new Quotation("Success is no accident. It is hard work, perseverance, learning, studying, sacrifice and most of all, love of what you are doing or learning to do. ", "Pele"),
  new Quotation("Your work is going to fill a large part of your life, and the only way to be truly satisfied is to do what you believe is great work. And the only way to do great work is to love what you do. If you haven't found it yet, keep looking. Don't settle. As with all matters of the heart, you'll know when you find it. ", "Steve Jobs")
]

function printRandomQuote() {
  // This could be less code, but I'm "spelling out" some things

  var count = arrayOfQuotations.length;
  var randomQuoteIndex = Math.floor(Math.random() * count);
  document.write(`"${arrayOfQuotations[randomQuoteIndex].quote}" - ${arrayOfQuotations[randomQuoteIndex].author}<br />`);  
}

printRandomQuote();

If you copy the above and paste it in your console, you can see it work in real-time.