CodeBlanks Logo Icon CodeBlanks

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.

Başlangıç kodu

index.js
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 metodunu ekle
  // buraya yaz
}
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);

Örnek çözüm

index.js
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);

Beklenen çıktı

Doğru: 1
Yüzde: 50

Soru ve cevaplar

Dersin sorularını cevaplarıyla birlikte inceleyebilirsin. Kendini denemek istersen dersi interaktif olarak çöz.

1 score(["Ankara", "İzmir"]) iki sorulu quiz'de ne döndürür?

index.js
quiz.score(["Ankara", "İzmir"])
  • correct: 1, percentage: 50 Doğru cevap
  • correct: 2, percentage: 100
  • correct: 0, percentage: 0
  • correct: 1, percentage: 25

Açıklama: Bir yanıt doğru (Ankara), biri yanlış (İzmir). 1/2 = %50.

2 Parçayı rolüne eşle

Kavram Anlamı
answers[i] Kullanıcının yanıtı
questions[i].answer Doğru yanıt
correct Doğru sayısı
percentage Yüzde

3 percentage hesabını tamamla

Soru
const percentage = Math.____((correct / total) * 100);
Cevap
const percentage = Math.round((correct / total) * 100);

Açıklama: Math.round en yakına yuvarlar.

Orta seviye bölümündeki tüm dersler