Skip to content

Scoring system #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions Quiz App Master/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,50 @@ input {
input::placeholder {
color: #aaa;
}
.end-container {
text-align: center;
padding: 2rem;
max-width: 600px;
margin: 0 auto;
}

.score-container {
background: #f8f9fa;
border-radius: 10px;
padding: 2rem;
margin: 2rem 0;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.score-text {
font-size: 1.5rem;
font-weight: bold;
margin-bottom: 1rem;
}

.correct-answers, .percentage {
font-size: 1.2rem;
margin: 0.5rem 0;
}

#feedbackMessage {
font-size: 1.3rem;
margin-top: 1.5rem;
color: #2c3e50;
font-weight: bold;
}

button {
background: #3498db;
color: white;
border: none;
padding: 0.8rem 1.5rem;
font-size: 1.1rem;
border-radius: 5px;
cursor: pointer;
transition: background 0.3s;
}

button:hover {
background: #2980b9;
}
125 changes: 103 additions & 22 deletions Quiz App Master/end.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,115 @@
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Congrats!</title>
<title>Quiz Results</title>
<link rel="stylesheet" href="app.css" />
<style>
.end-container {
text-align: center;
padding: 2rem;
max-width: 600px;
margin: 0 auto;
}

.score-container {
background: #f8f9fa;
border-radius: 10px;
padding: 2rem;
margin: 2rem 0;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.score-text {
font-size: 1.5rem;
font-weight: bold;
margin-bottom: 1rem;
}

.correct-answers, .percentage {
font-size: 1.2rem;
margin: 0.5rem 0;
}

#feedbackMessage {
font-size: 1.3rem;
margin-top: 1.5rem;
color: #2c3e50;
font-weight: bold;
}

.btn {
background: #3498db;
color: white;
border: none;
padding: 0.8rem 1.5rem;
font-size: 1.1rem;
border-radius: 5px;
cursor: pointer;
transition: background 0.3s;
margin: 0.5rem;
text-decoration: none;
display: inline-block;
}

.btn:hover {
background: #2980b9;
}

#username {
padding: 0.8rem;
font-size: 1.1rem;
border-radius: 5px;
border: 1px solid #ddd;
margin-right: 0.5rem;
}

.flex-center {
display: flex;
justify-content: center;
}

.flex-column {
flex-direction: column;
align-items: center;
}
</style>
</head>
<body>
<div class="container">
<div id="end" class="flex-center flex-column">
<h1 id="finalScore"></h1>
<form>
<input
type="text"
name="username"
id="username"
placeholder="username"
/>
<button
type="submit"
class="btn"
id="saveScoreBtn"
onclick="saveHighScore(event)"
disabled
>
Save
</button>
</form>
<a class="btn" href="/game.html">Play Again</a>
<a class="btn" href="/">Go Home</a>
<div class="end-container">

<div class="score-container">
<p class="score-text">Your score: <span id="finalScore"></span></p>
<p class="correct-answers">Correct answers: <span id="correctAnswers"></span>/<span id="totalQuestions"></span></p>
<p class="percentage">Percentage: <span id="percentageScore"></span>%</p>
<p class="feedback" id="feedbackMessage"></p>
</div>

<form id="saveScoreForm">
<input
type="text"
name="username"
id="username"
placeholder="username"
required
/>
<button
type="submit"
class="btn"
id="saveScoreBtn"
>
Save Score
</button>
</form>

<div class="action-buttons">
<a class="btn" href="/game.html">Play Again</a>
<a class="btn" href="/">Go Home</a>
</div>
</div>
</div>
</div>
<script src="end.js"></script>
</body>
</html>
</html>
65 changes: 53 additions & 12 deletions Quiz App Master/end.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,70 @@
const username = document.getElementById('username');
const saveScoreBtn = document.getElementById('saveScoreBtn');
// DOM Elements
const finalScore = document.getElementById('finalScore');
const correctAnswersEl = document.getElementById('correctAnswers');
const totalQuestionsEl = document.getElementById('totalQuestions');
const percentageScoreEl = document.getElementById('percentageScore');
const feedbackMessage = document.getElementById('feedbackMessage');
const usernameInput = document.getElementById('username');
const saveScoreBtn = document.getElementById('saveScoreBtn');
const saveScoreForm = document.getElementById('saveScoreForm');

// Get results from localStorage
const mostRecentScore = localStorage.getItem('mostRecentScore');
const correctAnswers = localStorage.getItem('correctAnswers');
const totalQuestions = localStorage.getItem('totalQuestions');
const percentageScore = localStorage.getItem('percentageScore');

// High Scores
const highScores = JSON.parse(localStorage.getItem('highScores')) || [];

const MAX_HIGH_SCORES = 5;

// Display results
finalScore.innerText = mostRecentScore;
correctAnswersEl.innerText = correctAnswers;
totalQuestionsEl.innerText = totalQuestions;
percentageScoreEl.innerText = percentageScore;

username.addEventListener('keyup', () => {
saveScoreBtn.disabled = !username.value;
// Add feedback based on percentage
const percentage = parseInt(percentageScore);
if (percentage >= 90) {
feedbackMessage.innerText = "Excellent! 🎉";
} else if (percentage >= 70) {
feedbackMessage.innerText = "Good job! 👍";
} else if (percentage >= 50) {
feedbackMessage.innerText = "Not bad! 😊";
} else {
feedbackMessage.innerText = "Keep practicing! 💪";
}

// Enable save button when username is entered
usernameInput.addEventListener('keyup', () => {
saveScoreBtn.disabled = !usernameInput.value;
});

saveHighScore = (e) => {
// Save high score
saveScoreForm.addEventListener('submit', (e) => {
e.preventDefault();

const score = {
score: mostRecentScore,
name: username.value,
correctAnswers: correctAnswers,
totalQuestions: totalQuestions,
percentage: percentageScore,
name: usernameInput.value,
date: new Date().toLocaleDateString()
};

highScores.push(score);
highScores.sort((a, b) => b.score - a.score);
highScores.splice(5);

highScores.splice(MAX_HIGH_SCORES);
localStorage.setItem('highScores', JSON.stringify(highScores));
window.location.assign('/');
};

// Disable form after submission
usernameInput.disabled = true;
saveScoreBtn.disabled = true;

// Show confirmation
feedbackMessage.textContent = `Score saved for ${usernameInput.value}!`;
feedbackMessage.style.color = "#4CAF50";
});
1 change: 1 addition & 0 deletions Quiz App Master/game.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ <h2 id="question"></h2>
</div>
</div>
</div>

<script src="game.js"></script>
</body>
</html>
Loading