$(document).on('ready', function() { var quote_selector = new QuoteSelector(); quote_selector.update_page(); $('#generate-button').click(function(event) { event.preventDefault(); quote_selector.update_page(); }); }); function QuoteSelector() { var current_quote_index this.update_page = function() { var selected_quote = select_random_quote(); update_html_text(selected_quote); update_tweet_link(selected_quote); }; function select_random_quote() { var random_number = randomize_number(); current_quote_index = random_number; return quotes_array[random_number]; } function randomize_number() { var quotes_count = quotes_array.length; var random_number = Math.floor(Math.random() * quotes_count); while (random_number == current_quote_index) { random_number = Math.floor(Math.random() * quotes_count); } return random_number; } function update_html_text(selected_quote) { $('#sentence').text(selected_quote.sentence) $('#book-title').text(selected_quote.title) $('#book-writer').text(selected_quote.writer) } function update_tweet_link(selected_quote) { $('#tweet-quote').attr('href', 'https://twitter.com/intent/tweet?text=' + selected_quote.sentence + ' - ' + selected_quote.title + ' by ' + selected_quote.writer); } } var quotes_array = [{ title: "Moby Dick", writer: "Herman Melville", sentence: '"Call me Ishmael."' }, { title: "Pride and Prejudice", writer: "Jane Austen", sentence: '"It is a truth universally acknowledged, that a single man in possession of a good fortune, must be in want of a wife."' }, { title: "Anna Karenina", writer: "Leo Tolstoy", sentence: '"All happy families are alike, each unhappy family is unhappy in its own way."' }, { title: "Anna Karenina", writer: "Leo Tolstoy", sentence: '"All happy families are alike, each unhappy family is unhappy in its own way."' }, { title: "Lolita", writer: "Vladimir Nabokov", sentence: '"Lolita, light of my life, fire of my loins."' }, { title: "1984", writer: "George Orwell", sentence: '"It was a bright cold day in April, and the clocks were striking thirteen."' }, { title: "Catcher in the Rye", writer: "J.D. Salinger", sentence: '"If you really want to hear about it, the first thing you\'ll probably want to know is where I was born, and what my lousy childhood was like, and how my parents were occupied and all before they had me, and all that David Copperfield kind of crap, but I don\'t feel like going into it, if you want to know the truth."' }, { title: "David Copperfield", writer: "Charles Dickens", sentence: '"Whether I shall turn out to be the hero of my own life, or whether that station will be held by anybody else, these pages must show."' }, { title: "Ulysses", writer: "James Joyce", sentence: '"Stately, plump Buck Mulligan came from the stairhead, bearing a bowl of lather on which a mirror and a razor lay crossed."' }, { title: "The Stranger", writer: "Albert Camus", sentence: '"Mother died today."' }, { title: "Notes from Underground", writer: "Fyodor Dostoyevsky", sentence: '"I am a sick man . . . I am a spiteful man."' }, { title: "Slaughterhouse-Five", writer: "Kurt Vonnegut", sentence: '"All this happened, more or less."' }, { title: "The Voyage of the Dawn Treader", writer: "C.S. Lewis", sentence: '"There was a boy called Eustace Clarence Scrubb, and he almost deserved it."' }, { title: "The Old Man and the Sea", writer: "Ernest Hemingway", sentence: '"He was an old man who fished alone in a skiff in the Gulf Stream and he had gone eighty-four days now without taking a fish."' }, { title: "The Great Gatsby", writer: "F. Scott Fitzgerald", sentence: '"In my younger and more vulnerable years my father gave me some advice that I\'ve been turning over in my mind ever since."' }, { title: "Love in the Time of Cholera", writer: "Gabriel García Márquez", sentence: '"It was inevitable: the scent of bitter almonds always reminded him of the fate of unrequited love."' }, { title: "Don Quixote", writer: "Miguel de Cervantes", sentence: '"Somewhere in la Mancha, in a place whose name I do not care to remember, a gentleman lived not long ago, one of those who has a lance and ancient shield on a shelf and keeps a skinny nag and a greyhound for racing."' }, { title: "To Kill a Mockingbird", writer: "Harper Lee", sentence: '"When he was nearly thirteen, my brother Jem got his arm badly broken at the elbow."' }, { title: "Lord Of The Rings", writer: "J.R.R. Tolkien", sentence: '"When Mr Bilbo Baggins of Bag End announced that he would shortly be celebrating his eleventy-first birthday with a party of special magnificence, there was much talk and excitement in Hobbiton."' }, { title: "Fear and loathing in Las Vegas", writer: "Hunter S. Thompson", sentence: '"We were somewhere around Barstow on the edge of the desert when the drugs began to take hold."' }, { title: "On the road", writer: "Jack kerouac", sentence: '"I first met Dean not long after my wife and I split up."' }, { title: "One Hundred Years of Solitude", writer: "Gabriel García Márquez", sentence: '"Many years later, as he faced the firing squad, Colonel Aureliano Buendía was to remember that distant afternoon when his father took him to discover ice"' }, { title: "Metamorphosis", writer: "Franz Kafka", sentence: '"As Gregor Samsa awoke one morning from uneasy dreams he found himself transformed in his bed into a monstrous vermin."' }, { title: "The Hobbit", writer: "J.R.R. Tolkien", sentence: '"In a hole in the ground there lived a hobbit."' }, { title: "The Hitchhiker’s Guide to the Galaxy", writer: "Douglas Adams", sentence: '"Far out in the uncharted backwaters of the unfashionable end of the Western Spiral arm of the Galaxy lies a small unregarded yellow sun."' }]

!