Skip to content

Commit

Permalink
Merge pull request #370 from SoulNikhar/main
Browse files Browse the repository at this point in the history
Update Quiz.js
  • Loading branch information
selemondev authored Nov 1, 2023
2 parents 017e940 + b825b2c commit 5663e53
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 73 deletions.
108 changes: 52 additions & 56 deletions Quiz/Quiz.js
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();
25 changes: 8 additions & 17 deletions url-shortner/script.js
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;
});

0 comments on commit 5663e53

Please sign in to comment.