|
1 | 1 | // DO NOT EDIT BELOW HERE |
2 | | - |
| 2 | +// |
3 | 3 | // pickFromArray is a function which will return one item, at |
4 | 4 | // random, from the given array. |
5 | | -// |
| 5 | + |
6 | 6 | // Parameters |
7 | 7 | // ---------- |
8 | 8 | // choices: an array of items to pick from. |
9 | 9 | // |
| 10 | + |
10 | 11 | // Returns |
11 | 12 | // ------- |
12 | 13 | // One item at random from the given array. |
13 | 14 | // |
| 15 | + |
14 | 16 | // Examples of use |
15 | 17 | // --------------- |
16 | 18 | // pickFromArray(['a','b','c','d']) // maybe returns 'c' |
17 | 19 |
|
18 | 20 | // 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)]; |
21 | 24 | } |
| 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. . |
22 | 40 |
|
23 | 41 | // A list of quotes you can use in your app. |
24 | 42 | // DO NOT modify this array, otherwise the tests may break! |
@@ -490,4 +508,9 @@ const quotes = [ |
490 | 508 | }, |
491 | 509 | ]; |
492 | 510 |
|
| 511 | +const buttonElem = document.querySelector("#new-quote"); |
| 512 | + |
| 513 | +buttonElem.addEventListener("click", updateWebpageWithQuote); |
| 514 | +updateWebpageWithQuote(); |
| 515 | + |
493 | 516 | // call pickFromArray with the quotes array to check you get a random quote |
0 commit comments