Skip to content

Commit d7eaca1

Browse files
author
Arthur
committed
quotes.js
1 parent 4222cda commit d7eaca1

1 file changed

Lines changed: 27 additions & 4 deletions

File tree

‎Sprint-3/quote-generator/quotes.js‎

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,42 @@
11
// DO NOT EDIT BELOW HERE
2-
2+
//
33
// pickFromArray is a function which will return one item, at
44
// random, from the given array.
5-
//
5+
66
// Parameters
77
// ----------
88
// choices: an array of items to pick from.
99
//
10+
1011
// Returns
1112
// -------
1213
// One item at random from the given array.
1314
//
15+
1416
// Examples of use
1517
// ---------------
1618
// pickFromArray(['a','b','c','d']) // maybe returns 'c'
1719

1820
// You don't need to change this function
19-
function pickFromArray(choices) {
20-
return choices[Math.floor(Math.random() * choices.length)];
21+
22+
function pickFromArray(quotes) {
23+
return quotes[Math.floor(Math.random() * quotes.length)];
2124
}
25+
// First condition: When a person click the button, it should generate back a quote;
26+
27+
function updateWebpageWithQuote() {
28+
const elem = document.querySelector("#quote");
29+
const elem2 = document.querySelector("#author");
30+
31+
// store into the variable
32+
const elem3 = pickFromArray(quotes);
33+
elem.innerText = elem3.quote;
34+
elem2.innerText = elem3.author;
35+
}
36+
37+
// Second condition: when a person enter the website, it should have a quote appear;
38+
39+
// Third condition: when a person clicks the button each time, it should have different quote. .
2240

2341
// A list of quotes you can use in your app.
2442
// DO NOT modify this array, otherwise the tests may break!
@@ -490,4 +508,9 @@ const quotes = [
490508
},
491509
];
492510

511+
const buttonElem = document.querySelector("#new-quote");
512+
513+
buttonElem.addEventListener("click", updateWebpageWithQuote);
514+
updateWebpageWithQuote();
515+
493516
// call pickFromArray with the quotes array to check you get a random quote

0 commit comments

Comments
 (0)