diff --git a/Quiz/Quiz.js b/Quiz/Quiz.js index 33ca8a1d..e310ac33 100644 --- a/Quiz/Quiz.js +++ b/Quiz/Quiz.js @@ -1,67 +1,63 @@ -const take = document.querySelector(".takeQuiz"); +const takeQuizContainer = document.querySelector(".takeQuiz"); -take.innerHTML = "" +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 = ` -

${num1} + ${num2} = ??

- - - ` - } else if (sing == 2) { - answer = num1 - num2; - take.innerHTML = ` -

${num1} - ${num2} = ??

- - - ` - } else if (sing == 3) { - answer = num1 * num2; - take.innerHTML = ` -

${num1} * ${num2} = ??

- - - ` - } else { - answer = num1 / num2; - take.innerHTML = ` -

${num1} / ${num2} = ??

- - - ` + takeQuizContainer.innerHTML = ` +

${num1} ${getSignSymbol(sing)} ${num2} = ??

+ + + `; +} + +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 = ` -

Your answer is correct

- - ` - } else { - take.innerHTML = ` -

Your answer is wrong

- - ` + +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 = `

Please enter a valid number.

`; + } else if (userAnswer === correctAnswer) { + takeQuizContainer.innerHTML = ` +

Your answer is correct

+ + `; + } else { + takeQuizContainer.innerHTML = ` +

Your answer is wrong

+ + `; + } } -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(); diff --git a/url-shortner/script.js b/url-shortner/script.js index 5330c079..18da2dc4 100644 --- a/url-shortner/script.js +++ b/url-shortner/script.js @@ -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}`; - }); -}); \ No newline at end of file +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; +});