1
+ const countries = [
2
+ { name : "France" , flag : "https://flagcdn.com/fr.svg" } ,
3
+ { name : "Germany" , flag : "https://flagcdn.com/de.svg" } ,
4
+ { name : "Italy" , flag : "https://flagcdn.com/it.svg" } ,
5
+ { name : "Japan" , flag : "https://flagcdn.com/jp.svg" } ,
6
+ { name : "Brazil" , flag : "https://flagcdn.com/br.svg" } ,
7
+ { name : "United States" , flag : "https://flagcdn.com/us.svg" } ,
8
+ { name : "Canada" , flag : "https://flagcdn.com/ca.svg" } ,
9
+ { name : "United Kingdom" , flag : "https://flagcdn.com/gb.svg" } ,
10
+ { name : "Australia" , flag : "https://flagcdn.com/au.svg" } ,
11
+ { name : "China" , flag : "https://flagcdn.com/cn.svg" } ,
12
+ { name : "India" , flag : "https://flagcdn.com/in.svg" } ,
13
+ { name : "Mexico" , flag : "https://flagcdn.com/mx.svg" } ,
14
+ { name : "Russia" , flag : "https://flagcdn.com/ru.svg" } ,
15
+ { name : "South Africa" , flag : "https://flagcdn.com/za.svg" } ,
16
+ { name : "Argentina" , flag : "https://flagcdn.com/ar.svg" } ,
17
+ { name : "South Korea" , flag : "https://flagcdn.com/kr.svg" } ,
18
+ { name : "Nigeria" , flag : "https://flagcdn.com/ng.svg" } ,
19
+ { name : "Egypt" , flag : "https://flagcdn.com/eg.svg" } ,
20
+ { name : "Spain" , flag : "https://flagcdn.com/es.svg" } ,
21
+ { name : "Portugal" , flag : "https://flagcdn.com/pt.svg" } ,
22
+ { name : "Netherlands" , flag : "https://flagcdn.com/nl.svg" } ,
23
+ { name : "Sweden" , flag : "https://flagcdn.com/se.svg" } ,
24
+ { name : "Norway" , flag : "https://flagcdn.com/no.svg" } ,
25
+ { name : "Greece" , flag : "https://flagcdn.com/gr.svg" } ,
26
+ { name : "Turkey" , flag : "https://flagcdn.com/tr.svg" } ,
27
+ { name : "Saudi Arabia" , flag : "https://flagcdn.com/sa.svg" } ,
28
+ { name : "New Zealand" , flag : "https://flagcdn.com/nz.svg" } ,
29
+ { name : "Switzerland" , flag : "https://flagcdn.com/ch.svg" } ,
30
+ { name : "Poland" , flag : "https://flagcdn.com/pl.svg" } ,
31
+ { name : "Ukraine" , flag : "https://flagcdn.com/ua.svg" } ,
32
+ { name : "Malaysia" , flag : "https://flagcdn.com/my.svg" } ,
33
+ { name : "Thailand" , flag : "https://flagcdn.com/th.svg" } ,
34
+ { name : "Vietnam" , flag : "https://flagcdn.com/vn.svg" } ,
35
+ { name : "Indonesia" , flag : "https://flagcdn.com/id.svg" } ,
36
+ { name : "Philippines" , flag : "https://flagcdn.com/ph.svg" } ,
37
+ { name : "Pakistan" , flag : "https://flagcdn.com/pk.svg" } ,
38
+ { name : "Bangladesh" , flag : "https://flagcdn.com/bd.svg" } ,
39
+ { name : "Iran" , flag : "https://flagcdn.com/ir.svg" } ,
40
+ { name : "Iraq" , flag : "https://flagcdn.com/iq.svg" } ,
41
+ { name : "Israel" , flag : "https://flagcdn.com/il.svg" } ,
42
+ { name : "Cuba" , flag : "https://flagcdn.com/cu.svg" } ,
43
+ { name : "Venezuela" , flag : "https://flagcdn.com/ve.svg" } ,
44
+ { name : "Colombia" , flag : "https://flagcdn.com/co.svg" } ,
45
+ { name : "Peru" , flag : "https://flagcdn.com/pe.svg" } ,
46
+ { name : "Chile" , flag : "https://flagcdn.com/cl.svg" } ,
47
+ { name : "Morocco" , flag : "https://flagcdn.com/ma.svg" } ,
48
+ { name : "Algeria" , flag : "https://flagcdn.com/dz.svg" } ,
49
+ { name : "Ethiopia" , flag : "https://flagcdn.com/et.svg" } ,
50
+ { name : "Kenya" , flag : "https://flagcdn.com/ke.svg" } ,
51
+ { name : "Tanzania" , flag : "https://flagcdn.com/tz.svg" } ,
52
+ { name : "Uganda" , flag : "https://flagcdn.com/ug.svg" }
53
+ ] ;
54
+
55
+ let currentCountryIndex = 0 ;
56
+ let currentScore = 0 ;
57
+
58
+ document . addEventListener ( 'DOMContentLoaded' , ( ) => {
59
+ const flagElement = document . getElementById ( 'country-flag' ) ;
60
+ const guessInput = document . getElementById ( 'guess' ) ;
61
+ const submitGuessButton = document . getElementById ( 'submitGuess' ) ;
62
+ const resultElement = document . getElementById ( 'result' ) ;
63
+ const nextCountryButton = document . getElementById ( 'nextBtn' ) ;
64
+ const emptyErrorVal = document . querySelector ( "#empty-error" ) ;
65
+ const userScoreVal = document . querySelector ( "#user-score-val" )
66
+ userScoreVal . textContent = currentScore ;
67
+
68
+ function showCountry ( index ) {
69
+ flagElement . src = countries [ index ] . flag ;
70
+ guessInput . value = '' ;
71
+ resultElement . textContent = '' ;
72
+ emptyErrorVal . textContent = '' ;
73
+ }
74
+
75
+ function checkGuess ( ) {
76
+ const userGuess = guessInput . value . trim ( ) . toLowerCase ( ) ;
77
+ const correctAnswer = countries [ currentCountryIndex ] . name . toLowerCase ( ) ;
78
+ if ( userGuess === correctAnswer ) {
79
+ resultElement . textContent = 'Correct!' ;
80
+ resultElement . style . color = 'green' ;
81
+ currentScore += 10 ;
82
+ userScoreVal . textContent = currentScore ;
83
+ } else {
84
+ resultElement . textContent = `Wrong! The correct answer is ${ countries [ currentCountryIndex ] . name } .` ;
85
+ resultElement . style . color = 'red' ;
86
+ }
87
+
88
+ }
89
+
90
+ function nextCountry ( ) {
91
+ currentCountryIndex = ( currentCountryIndex + 1 ) % countries . length ;
92
+ showCountry ( currentCountryIndex ) ;
93
+ }
94
+
95
+ submitGuessButton . addEventListener ( 'click' , ( ) => {
96
+ if ( guessInput . value == "" ) {
97
+ emptyErrorVal . textContent = "Input field cannot be empty" ;
98
+ } else {
99
+ checkGuess ( ) ;
100
+ emptyErrorVal . textContent = "" ;
101
+ }
102
+ } ) ;
103
+
104
+ guessInput . addEventListener ( 'keyup' , ( e ) => {
105
+ if ( e . key === 'Enter' ) {
106
+ if ( guessInput . value == "" ) {
107
+ emptyErrorVal . textContent = "Input field cannot be empty" ;
108
+ } else {
109
+ checkGuess ( ) ;
110
+ emptyErrorVal . textContent = "" ;
111
+ }
112
+ }
113
+ } ) ;
114
+
115
+ nextCountryButton . addEventListener ( 'click' , nextCountry ) ;
116
+
117
+ // Show the first country on page load
118
+ showCountry ( currentCountryIndex ) ;
119
+ } ) ;
0 commit comments