|
| 1 | +// Cooding Challenge 1 |
| 2 | + |
| 3 | +/* |
| 4 | +Let's build a simple poll app! |
| 5 | +A poll has a question, an array of options from which people can choose, and an array with the number of replies for each option. This data is stored in the starter object below. |
| 6 | +Here are your tasks: |
| 7 | +1. Create a method called 'registerNewAnswer' on the 'poll' object. The method does 2 things: |
| 8 | + 1.1. Display a prompt window for the user to input the number of the selected option. The prompt should look like this: |
| 9 | + What is your favourite programming language? |
| 10 | + 0: JavaScript |
| 11 | + 1: Python |
| 12 | + 2: Rust |
| 13 | + 3: C++ |
| 14 | + (Write option number) |
| 15 | + |
| 16 | + 1.2. Based on the input number, update the answers array. For example, if the option is 3, increase the value AT POSITION 3 of the array by 1. Make sure to check if the input is a number and if the number makes sense (e.g answer 52 wouldn't make sense, right?) |
| 17 | +
|
| 18 | +2. Call this method whenever the user clicks the "Answer poll" button. |
| 19 | +
|
| 20 | +3. Create a method 'displayResults' which displays the poll results. The method takes a string as an input (called 'type'), which can be either 'string' or 'array'. If type is 'array', simply display the results array as it is, using console.log(). This should be the default option. If type is 'string', display a string like "Poll results are 13, 2, 4, 1". |
| 21 | +
|
| 22 | +4. Run the 'displayResults' method at the end of each 'registerNewAnswer' method call. |
| 23 | +HINT: Use many of the tools you learned about in this and the last section 😉 |
| 24 | +BONUS: Use the 'displayResults' method to display the 2 arrays in the test data. Use both the 'array' and the 'string' option. Do NOT put the arrays in the poll object! So what shoud the `this` keyword look like in this situation? |
| 25 | +
|
| 26 | + * BONUS TEST DATA 1: [5, 2, 3] |
| 27 | + * BONUS TEST DATA 2: [1, 5, 3, 9, 6, 1] |
| 28 | + * GOOD LUCK 😀 |
| 29 | +*/ |
| 30 | + |
| 31 | +// const poll = { |
| 32 | +// question: "What is your favourite programming language?", |
| 33 | +// options: ["0: JavaScript", "1: Python", "2: Rust", "3: C++"], |
| 34 | +// answers: new Array(4).fill(0), |
| 35 | +// registerNewUser() { |
| 36 | +// let val = this.options; |
| 37 | +// const response = prompt( |
| 38 | +// `What is your favourite programming language? \n |
| 39 | +// 0: JavaScript \n |
| 40 | +// 1: Python \n |
| 41 | +// 2: Rust \n |
| 42 | +// 3: C++ \n |
| 43 | +// (Write option number)` |
| 44 | +// ); |
| 45 | +// if (response === val[0][0]) { |
| 46 | +// this.answers[0] += 1; |
| 47 | +// this.displayResults(this.answers); |
| 48 | +// } else if (response === val[1][0]) { |
| 49 | +// this.answers[1] += 1; |
| 50 | +// this.displayResults(this.answers); |
| 51 | +// } else if (response === val[2][0]) { |
| 52 | +// this.answers[2] += 1; |
| 53 | +// this.displayResults(this.answers); |
| 54 | +// } else if (response === val[3][0]) { |
| 55 | +// this.answers[3] += 1; |
| 56 | +// this.displayResults(this.answers); |
| 57 | +// } else { |
| 58 | +// console.log("Please choose from 0 - 3"); |
| 59 | +// } |
| 60 | +// }, |
| 61 | +// displayResults(type) { |
| 62 | +// console.log(`Poll results are ${type.join(", ")}`); |
| 63 | +// }, |
| 64 | +// }; |
| 65 | + |
| 66 | +// const answerFnc = poll.registerNewUser; |
| 67 | +// document.addEventListener("click", answerFnc.bind(poll)); |
| 68 | + |
| 69 | +const poll = { |
| 70 | + question: "What is your favourite programming language?", |
| 71 | + options: ["0: JavaScript", "1: Python", "2: Rust", "3: C++"], |
| 72 | + answers: new Array(4).fill(0), |
| 73 | + registerNewUser() { |
| 74 | + const answer = Number( |
| 75 | + prompt( |
| 76 | + `${this.question} \n${this.options.join("\n")}\n (Write option number)` |
| 77 | + ) |
| 78 | + ); |
| 79 | + typeof answer === "number" && |
| 80 | + answer < this.answers.length && |
| 81 | + this.answers[answer]++; |
| 82 | + |
| 83 | + this.displayResult(); |
| 84 | + this.displayResult("string"); |
| 85 | + }, |
| 86 | + displayResult(type = "array") { |
| 87 | + if (type === "array") { |
| 88 | + console.log(this.answers); |
| 89 | + } else if (typeof type === "string") { |
| 90 | + console.log(`Poll results are ${this.answers.join(", ")}`); |
| 91 | + } |
| 92 | + }, |
| 93 | +}; |
| 94 | + |
| 95 | +document |
| 96 | + .querySelector(".poll") |
| 97 | + .addEventListener("click", poll.registerNewUser.bind(poll)); |
| 98 | + |
| 99 | +poll.displayResult.call({ answers: [5, 2, 3] }, "string"); |
| 100 | +poll.displayResult.call({ answers: [1, 5, 3, 9, 6, 1] }, "string"); |
| 101 | +poll.displayResult(); |
0 commit comments