From 179ebbe162c33a63a874a2e38fd140bb10d9b3b1 Mon Sep 17 00:00:00 2001 From: Atiqa Naseer Date: Thu, 26 Mar 2026 00:02:19 +0100 Subject: [PATCH 1/2] week 11 assignment --- task-2/index.js | 153 ++++++++++++++++++++++++++++++++++++++- task-2/package-lock.json | 27 +++++++ task-2/package.json | 12 +-- task-2/test-quiz.sh | 23 ++++++ 4 files changed, 208 insertions(+), 7 deletions(-) create mode 100644 task-2/package-lock.json create mode 100755 task-2/test-quiz.sh diff --git a/task-2/index.js b/task-2/index.js index 02acffa..e7d1630 100644 --- a/task-2/index.js +++ b/task-2/index.js @@ -1 +1,152 @@ -// Write your code here. You may create as many files as you like. +import readline from 'readline/promises'; +import chalk from 'chalk'; + +const USE_MOCK = process.env.USE_MOCK === 'true'; +const TOKEN = process.env.GITHUB_TOKEN; +const TOTAL_QUESTIONS = 10; + +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout, +}); + +async function getQuestions() { + if (USE_MOCK || !TOKEN) { + return [ + { + question: 'What is the capital of France?', + options: ['London', 'Berlin', 'Paris', 'Madrid'], + correctAnswer: 3, + }, + { + question: 'Which planet is the Red Planet?', + options: ['Venus', 'Mars', 'Jupiter', 'Saturn'], + correctAnswer: 2, + }, + { + question: 'What is the largest ocean?', + options: ['Atlantic', 'Indian', 'Arctic', 'Pacific'], + correctAnswer: 4, + }, + { + question: 'Which country has the Great Wall?', + options: ['Japan', 'India', 'China', 'Thailand'], + correctAnswer: 3, + }, + { + question: 'Capital of Japan?', + options: ['Kyoto', 'Tokyo', 'Osaka', 'Hiroshima'], + correctAnswer: 2, + }, + { + question: 'Longest river in the world?', + options: ['Amazon', 'Nile', 'Yangtze', 'Mississippi'], + correctAnswer: 2, + }, + { + question: 'How many continents?', + options: ['5', '6', '7', '8'], + correctAnswer: 3, + }, + { + question: 'Capital of Australia?', + options: ['Sydney', 'Melbourne', 'Canberra', 'Brisbane'], + correctAnswer: 3, + }, + { + question: 'Smallest country?', + options: ['Monaco', 'Liechtenstein', 'Vatican City', 'San Marino'], + correctAnswer: 3, + }, + { + question: 'Capital of Brazil?', + options: ['Rio', 'Brasília', 'Salvador', 'São Paulo'], + correctAnswer: 2, + }, + ]; + } + + const prompt = `Generate exactly ${TOTAL_QUESTIONS} geography questions as JSON: +{ + "questions": [ + { + "question": "Question text?", + "options": ["opt1", "opt2", "opt3", "opt4"], + "correctAnswer": 1 + } + ] +}`; + + try { + const res = await fetch('https://models.inference.ai.azure.com/chat/completions', { + method: 'POST', + headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${TOKEN}` }, + body: JSON.stringify({ + model: 'gpt-4o-mini', + messages: [{ role: 'user', content: prompt }], + max_tokens: 1500, + }), + }); + + if (!res.ok) { + throw new Error(`HTTP ${res.status}`); + } + + const data = await res.json(); + const content = data.choices[0].message.content; + const jsonMatch = content.match(/\{[\s\S]*\}/); + if (!jsonMatch) { + throw new Error('Invalid LLM response format'); + } + + const parsedQuestions = JSON.parse(jsonMatch[0]).questions; + if (!Array.isArray(parsedQuestions) || parsedQuestions.length < TOTAL_QUESTIONS) { + throw new Error(`LLM must return at least ${TOTAL_QUESTIONS} questions`); + } + + return parsedQuestions.slice(0, TOTAL_QUESTIONS); + } catch (error) { + console.error('Error:', error.message); + process.exit(1); + } +} + +(async () => { + try { + console.log(chalk.yellow('\n=== QUIZ GAME ===\n')); + console.log(chalk.red('Welcome to the Quiz Game!')); + console.log(chalk.blue('Answer 10 questions by typing the number of your choice (1-4).\n')); + if (!USE_MOCK && !TOKEN) { + console.log(chalk.yellow('No token found, using mock questions.\n')); + } + + const quizQuestions = await getQuestions(); + let score = 0; + + for (let index = 0; index < TOTAL_QUESTIONS; index++) { + console.log(`Question ${index + 1}/${TOTAL_QUESTIONS}: ${quizQuestions[index].question}`); + quizQuestions[index].options.forEach((option, optionIndex) => + console.log(` ${optionIndex + 1}. ${option}`), + ); + + let answer; + while (!['1', '2', '3', '4'].includes(answer)) { + answer = await rl.question('Answer (1-4): '); + } + + const isCorrect = parseInt(answer) === quizQuestions[index].correctAnswer; + if (isCorrect) { + score++; + console.log(chalk.green('✓ Correct! (+1 point)\n')); + } else { + const correctNumber = quizQuestions[index].correctAnswer; + const correctText = quizQuestions[index].options[correctNumber - 1]; + console.log(chalk.red(`✗ Wrong! Correct answer: ${correctNumber}. ${correctText} (+0 points)\n`)); + } + } + + console.log(`\nFinal Score: ${score}/${TOTAL_QUESTIONS}\n`); + } finally { + rl.close(); + } +})(); diff --git a/task-2/package-lock.json b/task-2/package-lock.json new file mode 100644 index 0000000..8f0633a --- /dev/null +++ b/task-2/package-lock.json @@ -0,0 +1,27 @@ +{ + "name": "task-2", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "task-2", + "version": "1.0.0", + "dependencies": { + "chalk": "^5.6.2" + } + }, + "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" + } + } + } +} diff --git a/task-2/package.json b/task-2/package.json index a992f45..710f2fb 100644 --- a/task-2/package.json +++ b/task-2/package.json @@ -1,13 +1,13 @@ { "name": "task-2", "version": "1.0.0", - "description": "", + "description": "AI-powered quiz game", "main": "index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "start": "node index.js" }, - "keywords": [], - "author": "", - "license": "ISC", - "type": "module" + "type": "module", + "dependencies": { + "chalk": "^5.6.2" + } } \ No newline at end of file diff --git a/task-2/test-quiz.sh b/task-2/test-quiz.sh new file mode 100755 index 0000000..fc7a852 --- /dev/null +++ b/task-2/test-quiz.sh @@ -0,0 +1,23 @@ +#!/bin/bash +# Test runner for the quiz with mock data and simulated answers + +# Use all correct answers (1 2 3 2 2 2 3 3 3 2) to test a perfect score +echo "Testing quiz with mock data and simulated input (all correct answers)..." +echo "" + +# Simulate input: 10 correct answers (options vary per question) +{ + echo "3" # Q1: Paris (option 3) + echo "2" # Q2: Mars (option 2) + echo "4" # Q3: Pacific (option 4) + echo "3" # Q4: China (option 3) + echo "2" # Q5: Tokyo (option 2) + echo "2" # Q6: Nile (option 2) + echo "3" # Q7: 7 continents (option 3) + echo "3" # Q8: Canberra (option 3) + echo "3" # Q9: Vatican City (option 3) + echo "2" # Q10: Brasília (option 2) +} | USE_MOCK=true node index.js + +echo "" +echo "Test completed!" From 77c0b5e0b1465a0b88bccabf54ba094b326a49d3 Mon Sep 17 00:00:00 2001 From: Atiqa Naseer Date: Thu, 26 Mar 2026 00:04:21 +0100 Subject: [PATCH 2/2] week11 assignment --- task-1/Time.js | 43 +++++++++++++++++++++++++++++++++++++++- task-1/index.js | 19 ++++++++++++++++-- task-1/package-lock.json | 32 ++---------------------------- 3 files changed, 61 insertions(+), 33 deletions(-) diff --git a/task-1/Time.js b/task-1/Time.js index 97ca7e2..1cb6488 100644 --- a/task-1/Time.js +++ b/task-1/Time.js @@ -1,3 +1,44 @@ 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 RangeError('Time must be between 00:00:00 and 23:59:59'); + } + + this.#secondsFromMidnight = totalSeconds; + } + + getHours() { + return Math.floor(this.#secondsFromMidnight / 3600); + } + + getMinutes() { + return Math.floor((this.#secondsFromMidnight % 3600) / 60); + } + + getSeconds() { + return this.#secondsFromMidnight % 60; + } + + addSeconds(seconds) { + this.#secondsFromMidnight = + ((this.#secondsFromMidnight + seconds) % 86400 + 86400) % 86400; + } + + addMinutes(minutes) { + this.addSeconds(minutes * 60); + } + + addHours(hours) { + this.addSeconds(hours * 3600); + } + + toString() { + return `${String(this.getHours()).padStart(2, '0')}:${String( + this.getMinutes(), + ).padStart(2, '0')}:${String(this.getSeconds()).padStart(2, '0')}`; + } } \ No newline at end of file diff --git a/task-1/index.js b/task-1/index.js index cfa7c93..ed3a9df 100644 --- a/task-1/index.js +++ b/task-1/index.js @@ -1,4 +1,19 @@ import { Time } from './Time.js'; -const time = new Time(13, 37, 0); -console.log(time.toString()); // Output: "13:37:00" +const time1 = new Time(13, 37, 0); +console.log(time1.toString()); // Output: "13:37:00" + +const time2 = new Time(10, 20, 0); +console.log(time2.toString()); + +const myTime = new Time(12, 35, 0); +console.log(myTime.toString()); // 12:35:00 +myTime.getHours(); // 12 +myTime.getMinutes(); // 35 +myTime.getSeconds(); // 0 + +myTime.addMinutes(25); +console.log(myTime.toString()); // 13:00:00 + +myTime.addHours(12); +console.log(myTime.toString()); // 01:00:00 \ 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",