Node.js
Skor hesaplama
Skor hesaplama metoduyla doğru sayısını öğrenirsin.
score metodu ile skor hesaplama
Şimdi Quiz sınıfına bir score metodu ekleyeceğiz. Bu metod bir yanıt dizisi alır, her sorunun doğru yanıtını karşılaştırır ve sonuç döndürür. Örnek: score(answers) { let correct = 0; ... return { correct, total, percentage }; }.
answers[i] ile kullanıcının i. soruya verdiği yanıt alınır. this.questions[i].answer ile doğru yanıt karşılaştırılır. Eşitse correct bir artar. Sonra percentage = Math.round((correct / total) * 100) ile yüzde hesaplanır. Math.round sonucu en yakın tam sayıya yuvarlar. Dönen nesne üç özellik taşır: correct (doğru sayısı), total (toplam soru), percentage (yüzde). Bu, quiz sonucunun özetidir.
Math.round ile yuvarlama
Math.round(x) bir sayıyı en yakın tam sayıya yuvarlar. Math.round(4.4) 4, Math.round(4.6) 5 verir. Skor hesaplamasında (correct / total) * 100 genellikle kesirli bir sayıdır; Math.round onu tam sayı yapar.
Örnek: 3 sorudan 2'si doğruysa (2/3) * 100 = 66.666.... Math.round bunu 67 yapar. Bu, yüzdeyi kullanıcıya gösterirken anlamlı bir tam sayı verir. Math.floor her zaman aşağı, Math.ceil her zaman yukarı yuvarlar. Math.round en yakına gider; bu yüzde gösterimi için en uygun olandır. Sonuç nesnesi { correct, total, percentage } olarak döndürülür.
Uygulama score metodunu ekle
Önceki dersin çözümünü al. Quiz sınıfına score(answers) metodu ekle: let correct = 0; ile başla, for döngüsünde if (answers[i] === this.questions[i].answer) { correct++; }, son olarak const percentage = Math.round((correct / total) * 100); return { correct: correct, total: total, percentage: percentage };. Son satırlar hazır: const result = quiz.score(["Ankara", "İzmir"]) ve iki console.log.
Örnek çözümü göster
class Question {
constructor(id, text, answer) {
this.id = id;
this.text = text;
this.answer = answer;
}
}
class Quiz {
constructor() {
this.questions = [];
}
addQuestion(text, answer) {
const id = this.questions.length + 1;
this.questions.push(new Question(id, text, answer));
}
run() {
for (let i = 0; i < this.questions.length; i++) {
console.log("Soru " + this.questions[i].id + ": " + this.questions[i].text);
}
}
score(answers) {
let correct = 0;
for (let i = 0; i < this.questions.length; i++) {
if (answers[i] === this.questions[i].answer) {
correct++;
}
}
const total = this.questions.length;
const percentage = Math.round((correct / total) * 100);
return { correct: correct, total: total, percentage: percentage };
}
}
const quiz = new Quiz();
quiz.addQuestion("Başkent?", "Ankara");
quiz.addQuestion("En büyük şehir?", "İstanbul");
const result = quiz.score(["Ankara", "İzmir"]);
console.log("Doğru: " + result.correct);
console.log("Yüzde: " + result.percentage);
Kendini test et
-
score(["Ankara", "İzmir"]) iki sorulu quiz'de ne döndürür?
quiz.score(["Ankara", "İzmir"])
- correct: 1, percentage: 50
- correct: 2, percentage: 100
- correct: 0, percentage: 0
- correct: 1, percentage: 25
Cevabı göster
correct: 1, percentage: 50 — Bir yanıt doğru (Ankara), biri yanlış (İzmir). 1/2 = %50.
-
Parçayı rolüne eşle
- answers[i]
- Kullanıcının yanıtı
- questions[i].answer
- Doğru yanıt
- correct
- Doğru sayısı
- percentage
- Yüzde
-
percentage hesabını tamamla
const percentage = Math.____((correct / total) * 100);
Cevabı göster
const percentage = Math.round((correct / total) * 100);
Sıradaki ders: Birleştirme