mirror of
https://github.com/AmanTahiliani/gophercises.git
synced 2026-08-07 19:56:17 -04:00
Different solution for Gophercise 1
This commit is contained in:
40
1. Quiz Game Sol2/students/hackeryarn/myquiz/myquiz.go
Normal file
40
1. Quiz Game Sol2/students/hackeryarn/myquiz/myquiz.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package quiz
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/gophercises/quiz/students/hackeryarn/problem"
|
||||
)
|
||||
|
||||
// Quiz represents the quiz to be given to the user
|
||||
type Quiz struct {
|
||||
problems []problem.Problem
|
||||
rightAnswers int
|
||||
}
|
||||
|
||||
// Run runs the quiz for all the problems keeping track of correct answers
|
||||
func (q *Quiz) Run(w io.Writer, r io.Reader) {
|
||||
for _, problem := range q.problems {
|
||||
problem.AskQuestion(w)
|
||||
correct := problem.CheckAnswer(r)
|
||||
if correct {
|
||||
q.rightAnswers++
|
||||
}
|
||||
}
|
||||
|
||||
q.PrintResults(w)
|
||||
}
|
||||
|
||||
// PrintResults outputs the results of the quiz
|
||||
func (q Quiz) PrintResults(w io.Writer) {
|
||||
fmt.Fprintf(w, "You got %d questions right!\n", q.rightAnswers)
|
||||
}
|
||||
|
||||
// New creates a new quiz from the supplied slice of problems
|
||||
func New(problems []problem.Problem) Quiz {
|
||||
return Quiz{
|
||||
problems: problems,
|
||||
rightAnswers: 0,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user