1.增加4道新题。

2.为每一道题添加笔记输入功能且可以被保存。
3.为每一道题添加1-5分的智能打分功能并可以保存历史最高分。
pull/272/head
zhangyi-user 3 weeks ago
parent c92b0bc3a8
commit 46cb3be1fd

@ -9,6 +9,10 @@
<body>
<div class="quiz-container" id="quiz">
<div class="quiz-header">
<div class="question-info">
<span id="question-counter">1/8</span>
<span id="best-score">Best: 0/8</span>
</div>
<h2 id="question">Question text</h2>
<ul>
<li>
@ -31,8 +35,28 @@
<label for="d" id="d_text">Question</label>
</li>
</ul>
<div class="rating-section">
<label>Rate this question (1-5):</label>
<div class="stars" id="rating-stars">
<span class="star" data-value="1"></span>
<span class="star" data-value="2"></span>
<span class="star" data-value="3"></span>
<span class="star" data-value="4"></span>
<span class="star" data-value="5"></span>
</div>
<span id="current-rating">Not rated</span>
</div>
<div class="notes-section">
<label for="notes">Notes:</label>
<textarea id="notes" placeholder="Add your notes here..."></textarea>
<button id="save-notes" class="save-btn">Save Notes</button>
</div>
</div>
<div class="button-group">
<button id="submit">Submit</button>
</div>
<button id="submit">Submit</button>
</div>
<script src="script.js"></script>
</body>

@ -31,6 +31,38 @@ const quizData = [
d: "none of the above",
correct: "b",
},
{
question: "Which company developed the React library?",
a: "Google",
b: "Facebook (Meta)",
c: "Microsoft",
d: "Apple",
correct: "b",
},
{
question: "What is the default port number for Node.js?",
a: "3000",
b: "8080",
c: "5000",
d: "8000",
correct: "a",
},
{
question: "Which CSS property is used to change the text color?",
a: "background-color",
b: "font-color",
c: "color",
d: "text-color",
correct: "c",
},
{
question: "What does API stand for?",
a: "Application Programming Interface",
b: "Advanced Programming Interface",
c: "Automated Processing Interface",
d: "Application Process Integration",
correct: "a",
},
];
const quiz = document.getElementById('quiz')
@ -41,11 +73,22 @@ const b_text = document.getElementById('b_text')
const c_text = document.getElementById('c_text')
const d_text = document.getElementById('d_text')
const submitBtn = document.getElementById('submit')
const questionCounter = document.getElementById('question-counter')
const bestScoreEl = document.getElementById('best-score')
const ratingStars = document.querySelectorAll('.star')
const currentRatingEl = document.getElementById('current-rating')
const notesTextarea = document.getElementById('notes')
const saveNotesBtn = document.getElementById('save-notes')
let currentQuiz = 0
let score = 0
let currentRating = 0
let questionRatings = JSON.parse(localStorage.getItem('questionRatings')) || {}
let questionNotes = JSON.parse(localStorage.getItem('questionNotes')) || {}
let bestScore = parseInt(localStorage.getItem('bestScore')) || 0
loadQuiz()
updateBestScore()
function loadQuiz() {
deselectAnswers()
@ -57,6 +100,11 @@ function loadQuiz() {
b_text.innerText = currentQuizData.b
c_text.innerText = currentQuizData.c
d_text.innerText = currentQuizData.d
questionCounter.innerText = `${currentQuiz + 1}/${quizData.length}`
loadQuestionRating()
loadQuestionNotes()
}
function deselectAnswers() {
@ -75,7 +123,84 @@ function getSelected() {
return answer
}
function updateBestScore() {
bestScoreEl.innerText = `Best: ${bestScore}/${quizData.length}`
}
function saveBestScore() {
if (score > bestScore) {
bestScore = score
localStorage.setItem('bestScore', bestScore)
updateBestScore()
}
}
ratingStars.forEach(star => {
star.addEventListener('click', () => {
const value = parseInt(star.dataset.value)
currentRating = value
updateRatingDisplay()
saveQuestionRating()
})
star.addEventListener('mouseenter', () => {
const value = parseInt(star.dataset.value)
highlightStars(value)
})
star.addEventListener('mouseleave', () => {
highlightStars(currentRating)
})
})
function highlightStars(rating) {
ratingStars.forEach(star => {
const value = parseInt(star.dataset.value)
if (value <= rating) {
star.classList.add('active')
} else {
star.classList.remove('active')
}
})
}
function updateRatingDisplay() {
highlightStars(currentRating)
if (currentRating > 0) {
currentRatingEl.innerText = `Your rating: ${currentRating}/5`
} else {
currentRatingEl.innerText = 'Not rated'
}
}
function saveQuestionRating() {
questionRatings[currentQuiz] = currentRating
localStorage.setItem('questionRatings', JSON.stringify(questionRatings))
}
function loadQuestionRating() {
currentRating = questionRatings[currentQuiz] || 0
updateRatingDisplay()
}
saveNotesBtn.addEventListener('click', () => {
saveQuestionNotes()
alert('Notes saved successfully!')
})
function saveQuestionNotes() {
questionNotes[currentQuiz] = notesTextarea.value
localStorage.setItem('questionNotes', JSON.stringify(questionNotes))
}
function loadQuestionNotes() {
notesTextarea.value = questionNotes[currentQuiz] || ''
}
submitBtn.addEventListener('click', () => {
saveQuestionNotes()
saveQuestionRating()
const answer = getSelected()
if(answer) {
@ -88,11 +213,16 @@ submitBtn.addEventListener('click', () => {
if(currentQuiz < quizData.length) {
loadQuiz()
} else {
saveBestScore()
quiz.innerHTML = `
<h2>You answered ${score}/${quizData.length} questions correctly</h2>
<button onclick="location.reload()">Reload</button>
<div class="final-score">
<h2>You answered ${score}/${quizData.length} questions correctly</h2>
<p class="best-score">Best Score: ${bestScore}/${quizData.length}</p>
<button onclick="location.reload()">Play Again</button>
</div>
`
}
} else {
alert('Please select an answer before submitting!')
}
})

@ -11,9 +11,10 @@ body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
overflow: hidden;
min-height: 100vh;
overflow-y: auto;
margin: 0;
padding: 20px;
}
.quiz-container {
@ -21,17 +22,27 @@ body {
border-radius: 10px;
box-shadow: 0 0 10px 2px rgba(100, 100, 100, 0.1);
width: 600px;
max-width: 100%;
overflow: hidden;
}
.quiz-header {
padding: 4rem;
padding: 2rem 3rem;
}
.question-info {
display: flex;
justify-content: space-between;
margin-bottom: 1rem;
font-size: 0.9rem;
color: #666;
}
h2 {
padding: 1rem;
padding: 1rem 0;
text-align: center;
margin: 0;
font-size: 1.3rem;
}
ul {
@ -40,8 +51,8 @@ ul {
}
ul li {
font-size: 1.2rem;
margin: 1rem 0;
font-size: 1.1rem;
margin: 0.8rem 0;
}
ul li label {
@ -68,3 +79,97 @@ button:focus {
outline: none;
background-color: #5e3370;
}
.button-group {
display: flex;
gap: 1px;
}
.button-group button {
flex: 1;
}
.rating-section {
margin-top: 1.5rem;
padding-top: 1rem;
border-top: 1px solid #eee;
}
.rating-section label {
display: block;
margin-bottom: 0.5rem;
font-weight: 500;
}
.stars {
display: inline-block;
margin-right: 1rem;
}
.star {
font-size: 1.5rem;
color: #ddd;
cursor: pointer;
transition: color 0.2s;
}
.star:hover,
.star.active {
color: #f39c12;
}
#current-rating {
font-size: 0.9rem;
color: #666;
}
.notes-section {
margin-top: 1.5rem;
padding-top: 1rem;
border-top: 1px solid #eee;
}
.notes-section label {
display: block;
margin-bottom: 0.5rem;
font-weight: 500;
}
.notes-section textarea {
width: 100%;
padding: 0.8rem;
border: 1px solid #ddd;
border-radius: 5px;
font-family: inherit;
font-size: 0.9rem;
resize: vertical;
min-height: 80px;
}
.save-btn {
margin-top: 0.5rem;
padding: 0.6rem 1.2rem;
font-size: 0.9rem;
width: auto;
border-radius: 5px;
background-color: #27ae60;
}
.save-btn:hover {
background-color: #219a52;
}
.final-score {
text-align: center;
padding: 2rem;
}
.final-score h2 {
margin-bottom: 1rem;
}
.final-score .best-score {
font-size: 1.1rem;
color: #8e44ad;
margin: 1rem 0;
}

Loading…
Cancel
Save