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 trialfarouq siwoku
Full Stack JavaScript Techdegree Student 7,324 PointsRandom quote generator
Hi, i have written all the codes for the random quote generator but my click button only works when the page is refresh.
var quotesource = [
{
quote : "I love you the more in that I believe you had liked me for my own sake and for nothing else .",
name : "John Keats",
},
{
quote : "I love you the more in that I believe you had liked me for my own sake and for nothing else .But man is not made for defeat. A man can be destroyed but not defeated",
name : "Ernest Hemingway",
},
{
quote: "Start by doing what's necessary; then do what's possible; and suddenly you are doing the impossible.",
name:"Francis of Assisi"
},
{
quote:"Believe you can and you're halfway there.",
name:"Theodore Roosevelt"
},
{
quote:"It does not matter how slowly you go as long as you do not stop.",
name:"Confucius"
},
{
quote:"Our greatest weakness lies in giving up. The most certain way to succeed is always to try just one more time.",
name:"Thomas A. Edison"
},
{
quote:"The will to win, the desire to succeed, the urge to reach your full potential... these are the keys that will unlock the door to personal excellence.",
name:"Confucius"
},
{
quote:"Don't watch the clock; do what it does. Keep going.",
name:"Sam Levenson"
},
{
quote:"A creative man is motivated by the desire to achieve, not by the desire to beat others.",
name:"Ayn Rand"
},
{
quote:"A creative man is motivated by the desire to achieve, not by the desire to beat others.",
name:"Ayn Rand"
},
{
quote:"Expect problems and eat them for breakfast.",
name:"Alfred A. Montapert"
},
{
quote:"Start where you are. Use what you have. Do what you can.",
name:"Arthur Ashe"
},
{
quote:"Ever tried. Ever failed. No matter. Try Again. Fail again. Fail better.",
name:"Samuel Beckett"
},
{
quote:"Be yourself; everyone else is already taken.",
name:"Oscar Wilde"
},
{
quote:"Two things are infinite: the universe and human stupidity; and I'm not sure about the universe.",
name:"Albert Einstein"
},
{
quote:"Always remember that you are absolutely unique. Just like everyone else.",
name:"Margaret Mead"
},
{
quote:"Do not take life too seriously. You will never get out of it alive.",
name:"Elbert Hubbard"
},
{
quote:"People who think they know everything are a great annoyance to those of us who do.",
name:"Isaac Asimov"
},
{
quote:"Procrastination is the art of keeping up with yesterday.",
name:"Don Marquis"
},
{
quote:"Get your facts first, then you can distort them as you please.",
name:"Mark Twain"
},
{
quote:"A day without sunshine is like, you know, night.",
name:"Steve Martin"
},
{
quote:"My grandmother started walking five miles a day when she was sixty. She's ninety-seven now, and we don't know where the hell she is.",
name:"Ellen DeGeneres"
},
{
quote:"Don't sweat the petty things and don't pet the sweaty things.",
name:"George Carlin"
},
{
quote:"Always do whatever's next.",
name:"George Carlin"
},
{
quote:"Atheism is a non-prophet organization.",
name:"George Carlin"
},
{
quote:"Hapiness is not something ready made. It comes from your own actions.",
name:"Dalai Lama"
}
];
var colors = [
'#490A3D',
'#BD1550',
'#E97F02',
'#F8CA00',
'#8A9B0F',
'#69D2E7',
'#FA6900',
'#16a085',
'#27ae60',
'#2c3e50',
'#f39c12',
'#e74c3c',
'#9b59b6',
'#FB6964',
'#342224',
'#472E32',
'#77B1A9',
'#73A857'
];
let authors;
let quotes;
var message;
var randomColor = Math.floor(Math.random() * colors.length);
var randomNUmber = Math.floor(Math.random() * quotesource.length);
var buttonEl = document.querySelector("button")
var bodyEl = document.querySelector("body")
function print(message){
const outputDiv= document.querySelector(".output")
outputDiv.innerHTML = message
}
buttonEl.addEventListener("click", () =>{
for(let i = 0; i < quotesource.length; i++){
authors = quotesource[randomNUmber].name.fontsize(3);
quotes = quotesource[randomNUmber].quote;
for(let i = 0; i <colors.length; i++){
bodyEl.style.backgroundColor= colors[randomColor]
}
message = `${quotes} written by ${authors}`;
print(message)
}
})
2 Answers
Steven Parker
231,269 PointsThe button keeps working, but on each click it does exactly the same thing, so there's no visible change. The loops in the handler are also unnecessary, as they keep doing the same thing over and over.
You probably want to remove the loops, and then move the code that makes the random choices into the handler so it will pick different values on each push.