<script>
document.body.append(document.createElement('button'));
const poll = {
question: 'What is your favourite programming language?',
options: ['0:JavaScript', '1:Phyton', '2:Rust', '3:C++'],
answers: new Array(4).fill(0),
registerNewAnswer(){
const answer = Number(prompt(`${this.question}\n${this.options.join('\n')}\n(Write option number)`));
//Update answer //Error: *typeof
typeof answer === 'number' && answer < this.options.length && this.answers[answer]++;
//console.log(this.answers);
//error without this.displayResults
this.displayResults('string');
},
//error404 type set default to array
displayResults(type = 'array'){
if(type === 'array'){
console.log(this.answers);
}else if (type === 'string'){
console.log(`Poll results are ${this.answers}`);
};
},
};
document.querySelector('button').addEventListener('click', poll.registerNewAnswer.bind(poll));
</script>