Quiz Score Calculator

Score quiz answers and return a result summary.

Start building, submit solution and get feedback from the community.
2Submit Solution
5 upvotes10 upvotes

Quiz Score Calculator

Score a quiz by matching each question with the answer the user gave. The final result should include the count, total, percentage, and a simple message.

Write these functions:

  • isAnswerCorrect(question, userAnswer) should return true when the user's answer matches the correct answer.

  • countCorrectAnswers(questions, userAnswers) should match each question with its user answer and return the number correct.

  • calculatePercentage(correctCount, totalQuestions) should return the percentage score.

  • getResultMessage(percentage) should return a short message based on the percentage.

  • createQuizResult(questions, userAnswers) should return correctCount, totalQuestions, percentage, and message.

Sample checks:

js
const questions = [  { id: 1, correctAnswer: 'B' },  { id: 2, correctAnswer: 'A' },  { id: 3, correctAnswer: 'D' },  { id: 4, correctAnswer: 'C' },];const userAnswers = [  { questionId: 1, answer: 'B' },  { questionId: 2, answer: 'C' },  { questionId: 3, answer: 'D' },  { questionId: 4, answer: 'C' },];console.log(createQuizResult(questions, userAnswers));console.log(countCorrectAnswers(questions, userAnswers));console.log(calculatePercentage(3, questions.length));const partialAnswers = [{ questionId: 1, answer: 'B' }];console.log(createQuizResult(questions, partialAnswers));

Expected output:

txt
{ correctCount: 3, totalQuestions: 4, percentage: 75, message: "You passed" }375{ correctCount: 1, totalQuestions: 4, percentage: 25, message: "Keep practicing" }

If a user answer is missing, count that question as incorrect.

Join the Community

roadmap.sh is the most starred project on GitHub and is visited by hundreds of thousands of developers every month.

Rank  out of 28M!

361K

GitHub Stars

Star us on GitHub
Help us reach #1

+90k every month

+2.1M

Registered Users

Register yourself
Commit to your growth

+2k every month

49K

Discord Members

Join on Discord
Join the community

Roadmaps Best Practices Guides Videos FAQs YouTube

roadmap.sh by @kamrify

Community created roadmaps, best practices, projects, articles, resources and journeys to help you choose your path and grow in your career.

ThewNewStack

The top DevOps resource for Kubernetes, cloud-native computing, and large-scale development and deployment.