From 350abc7835b30d9ed2afa9b0e5ea562150dfcd11 Mon Sep 17 00:00:00 2001 From: julien Lengrand-Lambert Date: Mon, 28 Aug 2017 12:04:31 +0200 Subject: [PATCH] Add components private methods --- src/whose-flag-app/whose-flag-app.js | 31 ++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/whose-flag-app/whose-flag-app.js b/src/whose-flag-app/whose-flag-app.js index f5507fd..7602578 100644 --- a/src/whose-flag-app/whose-flag-app.js +++ b/src/whose-flag-app/whose-flag-app.js @@ -98,6 +98,37 @@ export class WhoseFlagApp extends Element { `; } + + _selectAnswer(event) { + let clickedButton = event.target; + this.userAnswer = clickedButton.textContent; + if (this.userAnswer == this.correctAnswer.name) { + this.outputMessage = `${this.userAnswer} is correct!`; + } + else { + this.outputMessage = `Nope! The correct answer is ${this.correctAnswer.name} !`; + } + } + + _handleResponse(event) { + this.countryList = event.detail.response.countrycodes; + while (!this.countryA || !this.countryB || (this.countryA.code == this.countryB.code)){ + this.countryA = this.countryList[this.__getRandomCountry()]; + this.countryB = this.countryList[this.__getRandomCountry()]; + } + this.correctAnswer = this.countryA; + + let coin = (Math.floor(Math.random() * 2)); + this.correctAnswer = coin == 1 ? this.countryA : this.countryB; + } + + __getRandomCountry() { + return Math.floor(Math.random() * (this.countryList.length)); + } + + _restart() { + window.location.reload(); + } } customElements.define('whose-flag-app', WhoseFlagApp); \ No newline at end of file