-
-
Notifications
You must be signed in to change notification settings - Fork 278
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #370 from SoulNikhar/main
Update Quiz.js
- Loading branch information
Showing
2 changed files
with
60 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,63 @@ | ||
const take = document.querySelector(".takeQuiz"); | ||
const takeQuizContainer = document.querySelector(".takeQuiz"); | ||
|
||
take.innerHTML = "<button onclick='quiz()'>Take Quiz</button>" | ||
function generateQuiz() { | ||
const num1 = randomNum(); | ||
const num2 = randomNum(); | ||
const sing = randomSign(); | ||
const answer = calculateAnswer(num1, num2, sing); | ||
|
||
let num1 = ""; | ||
let num2 = ""; | ||
let sing = ""; | ||
function quiz() { | ||
num1 = randomNum(); | ||
num2 = randomNum(); | ||
sing = randomSing(); | ||
let answer = ""; | ||
if (sing == 1) { | ||
answer = num1 + num2; | ||
take.innerHTML = ` | ||
<p>${num1} + ${num2} = ??</p> | ||
<input type="number"> | ||
<button onclick = 'submit(${answer},document.querySelector("input"))'>Submit</button> | ||
` | ||
} else if (sing == 2) { | ||
answer = num1 - num2; | ||
take.innerHTML = ` | ||
<p>${num1} - ${num2} = ??</p> | ||
<input type="number"> | ||
<button onclick = 'submit(${answer},document.querySelector("input"))'>Submit</button> | ||
` | ||
} else if (sing == 3) { | ||
answer = num1 * num2; | ||
take.innerHTML = ` | ||
<p>${num1} * ${num2} = ??</p> | ||
<input type="number"> | ||
<button onclick = 'submit(${answer},document.querySelector("input"))'>Submit</button> | ||
` | ||
} else { | ||
answer = num1 / num2; | ||
take.innerHTML = ` | ||
<p>${num1} / ${num2} = ??</p> | ||
<input type="number"> | ||
<button onclick = 'submit(${answer},document.querySelector("input"))'>Submit</button> | ||
` | ||
takeQuizContainer.innerHTML = ` | ||
<p>${num1} ${getSignSymbol(sing)} ${num2} = ??</p> | ||
<input type="number" id="userAnswer"> | ||
<button onclick="submitAnswer(${answer})">Submit</button> | ||
`; | ||
} | ||
|
||
function calculateAnswer(num1, num2, sing) { | ||
switch (sing) { | ||
case 1: return num1 + num2; | ||
case 2: return num1 - num2; | ||
case 3: return num1 * num2; | ||
case 4: return num1 / num2; | ||
default: return NaN; | ||
} | ||
} | ||
function submit(val, target) { | ||
if (target.value == val) { | ||
take.innerHTML = ` | ||
<p>Your answer is correct</p> | ||
<button onclick = 'retake()'>Re-Take</button> | ||
` | ||
} else { | ||
take.innerHTML = ` | ||
<p>Your answer is wrong</p> | ||
<button onclick = 'retake()'>Retry</button> | ||
` | ||
|
||
function getSignSymbol(sing) { | ||
switch (sing) { | ||
case 1: return '+'; | ||
case 2: return '-'; | ||
case 3: return '*'; | ||
case 4: return '/'; | ||
default: return ''; | ||
} | ||
} | ||
|
||
function retake() { | ||
quiz(); | ||
function submitAnswer(correctAnswer) { | ||
const userAnswerInput = document.getElementById("userAnswer"); | ||
const userAnswer = parseFloat(userAnswerInput.value); | ||
|
||
if (isNaN(userAnswer)) { | ||
takeQuizContainer.innerHTML = `<p>Please enter a valid number.</p>`; | ||
} else if (userAnswer === correctAnswer) { | ||
takeQuizContainer.innerHTML = ` | ||
<p>Your answer is correct</p> | ||
<button onclick="generateQuiz()">Re-Take</button> | ||
`; | ||
} else { | ||
takeQuizContainer.innerHTML = ` | ||
<p>Your answer is wrong</p> | ||
<button onclick="generateQuiz()">Retry</button> | ||
`; | ||
} | ||
} | ||
function randomSing() { | ||
return Math.floor(Math.random() * 4) + 1; | ||
|
||
function randomSign() { | ||
return Math.floor(Math.random() * 4) + 1; | ||
} | ||
|
||
function randomNum() { | ||
|
||
let ref = Math.floor(Math.random() * 100) + 1; | ||
return ref; | ||
return Math.floor(Math.random() * 100) + 1; | ||
} | ||
|
||
generateQuiz(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,8 @@ | ||
document.addEventListener("DOMContentLoaded", function () { | ||
const shortenButton = document.getElementById("shortenButton"); | ||
const originalUrlInput = document.getElementById("originalUrl"); | ||
const shortenedUrlText = document.getElementById("shortenedUrl"); | ||
|
||
shortenButton.addEventListener("click", function () { | ||
const originalUrl = originalUrlInput.value; | ||
if (originalUrl.trim() === "") { | ||
alert("Please enter a valid URL."); | ||
return; | ||
} | ||
|
||
// In a real implementation, you would send a request to a server to shorten the URL. | ||
// For simplicity, we'll just display the original URL here. | ||
shortenedUrlText.textContent = `Shortened URL: ${originalUrl}`; | ||
}); | ||
}); | ||
const originalUrlInput = document.getElementById("originalUrl"); | ||
const shortenButton = document.getElementById("shortenButton"); | ||
const shortenedUrlParagraph = document.getElementById("shortenedUrl"); | ||
shortenButton.addEventListener("click", () => { | ||
const originalUrl = originalUrlInput.value; | ||
const shortenedUrl = originalUrl; | ||
shortenedUrlParagraph.textContent = shortenedUrl; | ||
}); |