Quiz
This example shows how to create a small app functioning as a quiz.
Open in Online Editor
class MyApp extends App{
numberOfAnsweredQuestions = 0
numberOfCorrectAnswers = 0
createStartPage(){
return Question1Page
}
onCorrectAnswer(){
a.numberOfAnsweredQuestions += 1
a.numberOfCorrectAnswers += 1
}
onWrongAnswer(){
a.numberOfAnsweredQuestions += 1
}
reset(){
a.numberOfAnsweredQuestions = 0
a.numberOfCorrectAnswers = 0
}
}
class Question1Page extends Page{
createGui(){
return Rows.children(
Text.text(`Question 1`),
Text.text(`Which colors does the Swedish flag consist of?`)
.size(2),
Rows.size(1).children(
Columns.size(1).children(
Button.text(`Red and Blue`).size(1).onClick(a.onWrongAnswer),
Button.text(`Blue and Yellow`).size(1).onClick(a.onCorrectAnswer),
),
Columns.size(1).children(
Button.text(`Blue and lime`).size(1).onClick(a.onWrongAnswer),
Button.text(`White and Red`).size(1).onClick(a.onWrongAnswer),
),
),
)
}
createAfterDirections(){
return [
Direction.page(Question2Page).when(true).text(`On answer`),
]
}
}
class Question2Page extends Page{
createGui(){
return Rows.children(
Text.text(`Question 2`),
Text.text(`Which country has the smallest land size?`)
.size(2),
Rows.size(1).children(
Columns.size(1).children(
Button.text(`England`).size(1).onClick(a.onWrongAnswer),
Button.text(`Denmark`).size(1).onClick(a.onWrongAnswer),
),
Columns.size(1).children(
Button.text(`Germany`).size(1).onClick(a.onWrongAnswer),
Button.text(`Andorra`).size(1).onClick(a.onCorrectAnswer),
),
),
)
}
createAfterDirections(){
return [
Direction.page(Question3Page).when(true).text(`On answer`),
]
}
}
class Question3Page extends Page{
createGui(){
return Rows.children(
Text.text(`Question 3`),
Text.text(`In which country do they speak Spanish?`)
.size(2),
Rows.size(1).children(
Columns.size(1).children(
Button.text(`Spain`).size(1).onClick(a.onCorrectAnswer),
Button.text(`Italy`).size(1).onClick(a.onWrongAnswer),
),
Columns.size(1).children(
Button.text(`Russia`).size(1).onClick(a.onWrongAnswer),
Button.text(`Scotland`).size(1).onClick(a.onWrongAnswer),
),
),
)
}
createAfterDirections(){
return [
Direction.page(ResultPage).when(true).text(`On answer`),
]
}
}
class ResultPage extends Page{
createGui(){
let text = `You got ${a.numberOfCorrectAnswers} of ${a.numberOfAnsweredQuestions} correct.`
if(a.numberOfAnsweredQuestions == a.numberOfCorrectAnswers){
text = `You got all correct, good job!`
}else if(a.numberOfCorrectAnswers == 0){
text = `Oh, this was not impressive at all, 0 correct answers. Better luck next time!`
}
return Rows.children(
Text.text(`Result`),
Text.text(text).size(1),
Columns.children(
Space,
Button.text(`Try again`).page(Question1Page).onClick(a.reset),
Space,
),
)
}
}