From a2f7dfadca1a2d364121f61c4b94ba262dedfff4 Mon Sep 17 00:00:00 2001 From: Muna Nasher Date: Wed, 25 Mar 2026 21:24:02 +0100 Subject: [PATCH] c55-munaNasher-week11 --- package-lock.json | 6 +++ task-1/Time.js | 54 ++++++++++++++++++++++++- task-1/package-lock.json | 32 +-------------- task-2/index.js | 87 ++++++++++++++++++++++++++++++++++++++++ task-2/package-lock.json | 38 ++++++++++++++++++ task-2/package.json | 8 +++- 6 files changed, 192 insertions(+), 33 deletions(-) create mode 100644 package-lock.json create mode 100644 task-2/package-lock.json diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..6ad1e12 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,6 @@ +{ + "name": "c55-core-week-11", + "lockfileVersion": 3, + "requires": true, + "packages": {} +} diff --git a/task-1/Time.js b/task-1/Time.js index 97ca7e2..317a82a 100644 --- a/task-1/Time.js +++ b/task-1/Time.js @@ -1,3 +1,55 @@ export class Time { - // Your code here + #secondsFromMidnight; + + constructor(hours, minutes, seconds) { + const totalSeconds = hours * 3600 + minutes * 60 + seconds; + + if (totalSeconds < 0 || totalSeconds >= 86400) { + throw new Error("Invalid time"); + } + + this.#secondsFromMidnight = totalSeconds; + } + + // 🟢 getters + getHours() { + return Math.floor(this.#secondsFromMidnight / 3600); + } + + getMinutes() { + return Math.floor((this.#secondsFromMidnight % 3600) / 60); + } + + getSeconds() { + return this.#secondsFromMidnight % 60; + } + + // 🟢 normalize helper + #normalize() { + this.#secondsFromMidnight = + ((this.#secondsFromMidnight % 86400) + 86400) % 86400; + } + + // 🟢 add methods + addSeconds(seconds) { + this.#secondsFromMidnight += seconds; + this.#normalize(); + } + + addMinutes(minutes) { + this.addSeconds(minutes * 60); + } + + addHours(hours) { + this.addSeconds(hours * 3600); + } + + // 🟢 format as HH:mm:ss + toString() { + const hh = String(this.getHours()).padStart(2, "0"); + const mm = String(this.getMinutes()).padStart(2, "0"); + const ss = String(this.getSeconds()).padStart(2, "0"); + + return `${hh}:${mm}:${ss}`; + } } \ No newline at end of file diff --git a/task-1/package-lock.json b/task-1/package-lock.json index aba2728..14d0d66 100644 --- a/task-1/package-lock.json +++ b/task-1/package-lock.json @@ -161,9 +161,6 @@ "cpu": [ "arm64" ], - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -180,9 +177,6 @@ "cpu": [ "arm64" ], - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -199,9 +193,6 @@ "cpu": [ "ppc64" ], - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -218,9 +209,6 @@ "cpu": [ "s390x" ], - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -237,9 +225,6 @@ "cpu": [ "x64" ], - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -256,9 +241,6 @@ "cpu": [ "x64" ], - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -706,9 +688,6 @@ "cpu": [ "arm64" ], - "libc": [ - "glibc" - ], "license": "MPL-2.0", "optional": true, "os": [ @@ -729,9 +708,6 @@ "cpu": [ "arm64" ], - "libc": [ - "musl" - ], "license": "MPL-2.0", "optional": true, "os": [ @@ -752,9 +728,6 @@ "cpu": [ "x64" ], - "libc": [ - "glibc" - ], "license": "MPL-2.0", "optional": true, "os": [ @@ -775,9 +748,6 @@ "cpu": [ "x64" ], - "libc": [ - "musl" - ], "license": "MPL-2.0", "optional": true, "os": [ @@ -885,6 +855,7 @@ "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-4.0.4.tgz", "integrity": "sha512-QP88BAKvMam/3NxH6vj2o21R6MjxZUAd6nlwAS/pnGvN9IVLocLHxGYIzFhg6fUQ+5th6P4dv4eW9jX3DSIj7A==", "license": "MIT", + "peer": true, "engines": { "node": ">=12" }, @@ -1032,6 +1003,7 @@ "resolved": "https://registry.npmjs.org/vite/-/vite-8.0.2.tgz", "integrity": "sha512-1gFhNi+bHhRE/qKZOJXACm6tX4bA3Isy9KuKF15AgSRuRazNBOJfdDemPBU16/mpMxApDPrWvZ08DcLPEoRnuA==", "license": "MIT", + "peer": true, "dependencies": { "lightningcss": "^1.32.0", "picomatch": "^4.0.3", diff --git a/task-2/index.js b/task-2/index.js index 02acffa..7e7f6b6 100644 --- a/task-2/index.js +++ b/task-2/index.js @@ -1 +1,88 @@ // Write your code here. You may create as many files as you like. +import chalk from 'chalk'; +import readlineSync from 'readline-sync'; + +// 📝 Quiz questions (fake JSON for testing, can replace with API later) +const questions = [ + { + question: "What is the largest planet in our solar system?", + answers: ["Earth", "Mars", "Jupiter", "Saturn"], + correct: 3 + }, + { + question: "What is the capital of France?", + answers: ["Paris", "Rome", "Madrid", "Berlin"], + correct: 1 + }, + { + question: "Which chemical element has the symbol O?", + answers: ["Gold", "Oxygen", "Iron", "Hydrogen"], + correct: 2 + }, + { + question: "Who wrote the novel '1984'?", + answers: ["George Orwell", "Tolstoy", "Shakespeare", "Mark Twain"], + correct: 1 + }, + { + question: "What is the fastest land animal?", + answers: ["Cheetah", "Lion", "Gazelle", "Kangaroo"], + correct: 1 + }, + { + question: "Which is the largest ocean on Earth?", + answers: ["Atlantic Ocean", "Pacific Ocean", "Indian Ocean", "Arctic Ocean"], + correct: 2 + }, + { + question: "Which country uses the Euro as its currency?", + answers: ["United Kingdom", "Sweden", "Spain", "Switzerland"], + correct: 3 + }, + { + question: "Which organ pumps blood throughout the body?", + answers: ["Liver", "Lungs", "Heart", "Stomach"], + correct: 3 + }, + { + question: "Which animal is known as the 'King of the Jungle'?", + answers: ["Tiger", "Lion", "Elephant", "Leopard"], + correct: 2 + }, + { + question: "What is the fastest type of light?", + answers: ["X-rays", "Visible light", "Laser", "Light itself"], + correct: 4 + } +]; + +// 🟢 Start the game +console.log(chalk.blue("Welcome to the Quiz Game!")); +console.log("Answer 10 questions by typing the number of your choice (1-4)."); + +let score = 0; + +for (let i = 0; i < questions.length; i++) { + const q = questions[i]; + console.log(`\nQuestion ${i + 1}: ${q.question}`); + q.answers.forEach((ans, index) => { + console.log(`${index + 1}. ${ans}`); + }); + + let answer; + while (true) { + answer = readlineSync.question("Your answer (1-4): "); + if (["1","2","3","4"].includes(answer)) break; + console.log(chalk.yellow("Please enter a number from 1 to 4.")); + } + + if (parseInt(answer) === q.correct) { + console.log(chalk.green("Correct!")); + score++; + } else { + console.log(chalk.red(`Wrong! The correct answer is: ${q.answers[q.correct - 1]}`)); + } +} + +// 🟢 End of game +console.log(chalk.blue(`\nGame Over! Your final score: ${score}/${questions.length}`)); \ No newline at end of file diff --git a/task-2/package-lock.json b/task-2/package-lock.json new file mode 100644 index 0000000..fd236f0 --- /dev/null +++ b/task-2/package-lock.json @@ -0,0 +1,38 @@ +{ + "name": "task-2", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "task-2", + "version": "1.0.0", + "license": "ISC", + "dependencies": { + "chalk": "^5.6.2", + "readline-sync": "^1.4.10" + } + }, + "node_modules/chalk": { + "version": "5.6.2", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-5.6.2.tgz", + "integrity": "sha512-7NzBL0rN6fMUW+f7A6Io4h40qQlG+xGmtMxfbnH/K7TAtt8JQWVQK+6g0UXKMeVJoyV5EkkNsErQ8pVD3bLHbA==", + "license": "MIT", + "engines": { + "node": "^12.17.0 || ^14.13 || >=16.0.0" + }, + "funding": { + "url": "https://github.com/chalk/chalk?sponsor=1" + } + }, + "node_modules/readline-sync": { + "version": "1.4.10", + "resolved": "https://registry.npmjs.org/readline-sync/-/readline-sync-1.4.10.tgz", + "integrity": "sha512-gNva8/6UAe8QYepIQH/jQ2qn91Qj0B9sYjMBBs3QOB8F2CXcKgLxQaJRP76sWVRQt+QU+8fAkCbCvjjMFu7Ycw==", + "license": "MIT", + "engines": { + "node": ">= 0.8.0" + } + } + } +} diff --git a/task-2/package.json b/task-2/package.json index a992f45..df42620 100644 --- a/task-2/package.json +++ b/task-2/package.json @@ -9,5 +9,9 @@ "keywords": [], "author": "", "license": "ISC", - "type": "module" -} \ No newline at end of file + "type": "module", + "dependencies": { + "chalk": "^5.6.2", + "readline-sync": "^1.4.10" + } +}