28
loading...
This website collects cookies to deliver better user experience
div
is the 'Next' button.const [countries, setCountries] = useState([])
useEffect (() => {
fetch('https://restcountries.com/v3.1/all')
.then(res => res.json())
.then(data => {
const unMemberCountries = data.filter((country) {
country.unMember === true
}
setCountries(unMemberCountries)
})
}, [] )
const [flagQuizCountry, setFlagQuizCountry] = useState(countries[Math.floor(Math.random()*countries.length)])
const [incorrectFlagOne, setIncorrectFlagOne] = useState(countries[Math.floor(Math.random()*countries.length)])
const [incorrectFlagTwo, setIncorrectFlagTwo] = useState(countries[Math.floor(Math.random()*countries.length)])
const [incorrectFlagThree, setIncorrectFlagThree] = useState(countries[Math.floor(Math.random()*countries.length)])
const [flagResponseGiven, setFlagResponseGiven] = useState(false)
const [currentFlagQuestion, setCurrentFlagQuestion] = useState(0)
const [flagQuizScore, setFlagQuizScore] = useState (0)
div
, add your 'Next' button.<div'>
<ul style={{display: 'flex', flexDirection: 'row', justifyContent: 'center'}}
<li>
<button
value={flagQuizCountry.name}
onClick={handleFlagAnswer} >
{flagQuizCountry.name}
</button>
</li>
<li>
<button
value={incorrectOne.name}
onClick={handleFlagAnswer} >
{incorrectOne.name}
</button>
</li>
<li>
<button
value={incorrectTwo.name}
onClick={handleFlagAnswer} >
{incorrectTwo.name}
</button>
</li>
<li>
<button
value={incorrectThree.name}
onClick={handleFlagAnswer} >
{incorrectThree.name}
</button>
</li>
</ul
</div>
<div>
<button onClick={flagOnClick}>Next</button>
</div>
li
tag, and place all the buttons inside a ul
tag. Then use this code to randomize the order of the items in the list.function randomizeItems(items) {
let cached = items.slice(0), temp, i = cached.length, rand;
while(--i) {
rand = Math.floor(i * Math.random());
temp = cached[rand];
cached[rand] = cached[i];
cached[i] = temp;
}
return cached;
}
function randomizeList() {
let list = document.getElementById("capital-quiz-buttons");
let nodes = list.children, i = 0;
nodes = Array.prototype.slice.call(nodes);
nodes = randomizeItems(nodes);
while(i < nodes.length) {
list.appendChild(nodes[i]);
++i;
}
}
ul
in the following fashion to take away the bullet points.ul {
list-style-type: none;
padding: 0;
}
onClick
event to each button which calls something similar to the following function.function handleFlagAnswer (event) {
if (flagResponseGiven === false) {
if (event.target.value === flagQuizCountry.name.common) {
setFlagQuizScore(flagQuizScore + 1)
}
setFlagResponseGiven(true)
}
}
flagQuizCountry
, a point will be added the flagQuizScore
.function setNextFlagQuestion () {
setFlagQuizCountry (countryData[Math.floor(Math.random()*countryData.length)])
setIncorrectFlagOne (countryData[Math.floor(Math.random()*countryData.length)])
setIncorrectFlagTwo (countryData[Math.floor(Math.random()*countryData.length)])
setIncorrectFlagThree (countryData[Math.floor(Math.random()*countryData.length)])
let nextQuestion = currentFlagQuestion + 1
setCurrentFlagQuestion(nextQuestion)
setFlagResponseGiven(false)
resetButtonColors()
if (currentFlagQuestion >= 25){
// insert desired outcome
}
}