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

Jayson Camacho
seal-mask
.a{fill-rule:evenodd;}techdegree
Jayson Camacho
Full Stack JavaScript Techdegree Student 3,006 Points

Getting my print function to print on my site when the button is clicked

I'm having a hard time to have the function to print when it is clicked... this is what I have so far... Help Please.

// event listener to respond to "Show another quote" button clicks // when user clicks anywhere on the button, the "printQuote" function is called

var quotes = [ { quote : 'At the end of the day, you’ve gotta feel some way. So why not feel unbeatable? Why not feel untouchable?', source : 'Conor McGregor' }, { quote : 'All that we are is a result of what we have thought.', source : 'Buddha' }, { quote : 'Everything you want is out there waiting for you to ask. Everything you want also wants you. But you have to take action to get it.', source : 'Jack Canfield' }, { quote : 'It’s really important that you feel good. Because this feeling good is what goes out as a signal into the universe and starts to attract more of itself to you. So the more you can feel good, the more you will attract the things that help you feel good and that will keep bringing you up higher and higher.', source : 'Joe Vitale' }, { quote : 'A person is what he or she thinks about all day long.', source : 'Ralph Waldo Emerson' }, { quote : 'Your imagination is your preview of life’s coming attractions.', source : 'Albert Einstein' }, { quote : 'Nothing external to me has any power over me.', source : 'Walt Whitman' }, { quote : 'Happiness is not a destination. It is a method of life.', source : 'Burton Hills' }, { quote : 'Doubt is only removed by action. If you’re not working then that’s where doubt comes in.', source : 'Conor McGregor' } ];

var quote; var source;

function getRandomQuote() { var sourceLength = quotes.length; var randomNumber = Math.floor(Math.random()*sourceLength); for (var i = 0; i <= sourceLength; i += 1) { var newQuoteText = quotes[randomNumber].quote; var newQuoter = quotes[randomNumber].source; var leeQuote = newQuoteText + newQuoter; console.log(leeQuote); return; } }

document.getElementById('loadQuote').addEventListener("click", printQuote, false);

function printQuote(message) { document.getElementById('quote-box').innerHTML = message;

}

var message = getRandomQuote(); printQuote(message);

2 Answers

Peter Quin
Peter Quin
3,203 Points

This seemed to work for me, it took me a while to pick through it all. I understand the thought process that went into, for instance, the 'for loop' but the random number generator is doing the work of picking out the quote of choice each time so it wasn't needed. The event listener and innerHTML bits were, I think, just a bit miss-applied.

I'm no expert though so others may be able to elucidate more on what was exactly going wrong.

var quotes = [
        { quote : 'At the end of the day, you’ve gotta feel some way. So why not feel unbeatable? Why not feel untouchable?', source : 'Conor McGregor' },
        { quote : 'All that we are is a result of what we have thought.', source : 'Buddha' },
        { quote : 'Everything you want is out there waiting for you to ask. Everything you want also wants you. But you have to take action to get it.', source : 'Jack Canfield' },
        { quote : 'It’s really important that you feel good. Because this feeling good is what goes out as a signal into the universe and starts to attract more of itself to you. So the more you can feel good, the more you will attract the things that help you feel good and that will keep bringing you up higher and higher.', source : 'Joe Vitale' },
        {quote : 'A person is what he or she thinks about all day long.', source : 'Ralph Waldo Emerson' },
        { quote : 'Your imagination is your preview of life’s coming attractions.', source : 'Albert Einstein' }, { quote : 'Nothing external to me has any power over me.', source : 'Walt Whitman' },
        { quote : 'Happiness is not a destination. It is a method of life.', source : 'Burton Hills' },
        { quote : 'Doubt is only removed by action. If you’re not working then that’s where doubt comes in.', source : 'Conor McGregor' }
    ];

    var randomQuote;

    function getRandomQuote() {
        var sourceLength = quotes.length;
        var randomNumber = Math.floor(Math.random()*sourceLength);

        //you are wanting to log 1 random quote, so you dont need to cycle through all the quotes with a for loop you just need to pick one, therefore...

        var newQuoteText = quotes[randomNumber].quote;
        var newQuoter = quotes[randomNumber].source;
        randomQuote = '<p>' + newQuoteText + " " + newQuoter + '</p>';

    }


        // Im unsure quite what was happening here but you can combine it all into one function so as to link the 'load-quote' and 'quote-box' together
    document.getElementById('load-quote').addEventListener("click", function() {
        getRandomQuote();
        document.getElementById('quote-box').innerHTML = randomQuote;
    });