From 89d29db4f1db99738c7f43649c653da29d4dfe3f Mon Sep 17 00:00:00 2001 From: Jawad Al Bdiwi Date: Wed, 25 Mar 2026 18:46:13 +0100 Subject: [PATCH 1/2] assignment task 1 complete --- task-1/Time.js | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/task-1/Time.js b/task-1/Time.js index 97ca7e2..2868d4a 100644 --- a/task-1/Time.js +++ b/task-1/Time.js @@ -1,3 +1,47 @@ export class Time { - // Your code here -} \ No newline at end of file + #secondsFromMidnight; + constructor(hours, minutes, seconds) { + this.#secondsFromMidnight = hours * 3600 + minutes * 60 + seconds; + this.#normalize(); + } + + #normalize() { + this.#secondsFromMidnight %= 86400; + if (this.#secondsFromMidnight < 0) { + this.#secondsFromMidnight += 86400; + } + } + + getSeconds() { + return this.#secondsFromMidnight % 60; + } + + getMinutes() { + return Math.floor((this.#secondsFromMidnight % 3600) / 60); + } + + getHours() { + return Math.floor(this.#secondsFromMidnight / 3600); + } + + addSeconds(seconds) { + this.#secondsFromMidnight += seconds; + this.#normalize(); + } + + addMinutes(minutes) { + this.addSeconds(minutes * 60); + } + + addHours(hours) { + this.addSeconds(hours * 3600); + } + + 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}`; + } +} From 363c25c5c9f60e497aa4be25ba587a2d95c95aed Mon Sep 17 00:00:00 2001 From: Jawad Al Bdiwi Date: Thu, 26 Mar 2026 17:50:21 +0100 Subject: [PATCH 2/2] completed task 2 --- task-2/index.js | 148 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 147 insertions(+), 1 deletion(-) diff --git a/task-2/index.js b/task-2/index.js index 02acffa..7ec320d 100644 --- a/task-2/index.js +++ b/task-2/index.js @@ -1 +1,147 @@ -// Write your code here. You may create as many files as you like. +import dotenv from "dotenv"; +import OpenAI from "openai"; +import chalk from "chalk"; +import readlineSync from "readline-sync"; + +dotenv.config(); + +const client = new OpenAI({ + baseURL: "https://models.github.ai/inference/", + apiKey: process.env.GITHUB_TOKEN, +}); + +async function generateQuizQuestions() { + const prompt = ` +Generate exactly 10 multiple-choice quiz questions about world geography. + +Rules: +- Each question must have exactly 4 answer options. +- Only one answer is correct. +- Make the questions clear and suitable for a CLI quiz game. +- Return ONLY valid JSON. +- Do not include markdown code fences. +- Do not include any explanation before or after the JSON. + +Use this exact JSON format: +[ + { + "question": "Which country has the city of Kyoto?", + "options": ["China", "Japan", "South Korea", "Thailand"], + "correctAnswer": 2 + } +] + +Important: +- "correctAnswer" must be a number from 1 to 4. +- Return exactly 10 question objects. +`; + + const response = await client.chat.completions.create({ + model: "openai/gpt-4o-mini", + messages: [ + { + role: "user", + content: prompt, + }, + ], + temperature: 0.7, + }); + + const text = response.choices[0].message.content; + + try { + const questions = JSON.parse(text); + + const isValid = + Array.isArray(questions) && + questions.length === 10 && + questions.every( + (q) => + typeof q.question === "string" && + Array.isArray(q.options) && + q.options.length === 4 && + q.options.every((option) => typeof option === "string") && + Number.isInteger(q.correctAnswer) && + q.correctAnswer >= 1 && + q.correctAnswer <= 4, + ); + + if (!isValid) { + throw new Error("Quiz data is not in the correct format."); + } + + return questions; + } catch (error) { + console.log(chalk.red("Failed to parse quiz questions from the AI.")); + console.log(chalk.red("Raw response:")); + console.log(text); + throw error; + } +} + +function askQuestion(questionObj, questionNumber) { + console.log(chalk.blue(`\nQuestion ${questionNumber}/10`)); + console.log(chalk.yellow(questionObj.question)); + + questionObj.options.forEach((option, index) => { + console.log(`${index + 1}. ${option}`); + }); + + let userAnswer; + + while (true) { + userAnswer = readlineSync.question("\nYour answer (1-4): ").trim(); + + if (["1", "2", "3", "4"].includes(userAnswer)) { + break; + } + + console.log(chalk.red("Please type 1, 2, 3, or 4.")); + } + + return Number(userAnswer); +} + +function checkAnswer(questionObj, userAnswer) { + const correct = userAnswer === questionObj.correctAnswer; + + if (correct) { + console.log(chalk.green("Correct! +1 point")); + return 1; + } else { + const correctText = questionObj.options[questionObj.correctAnswer - 1]; + console.log( + chalk.red( + `Wrong! The correct answer was ${questionObj.correctAnswer}. ${correctText}`, + ), + ); + return 0; + } +} + +async function startQuiz() { + console.log(chalk.cyan("Welcome to the AI Powered Quiz Game!")); + console.log(chalk.cyan("Generating 10 quiz questions...\n")); + + let questions; + + try { + questions = await generateQuizQuestions(); + } catch (error) { + console.log(chalk.red("Could not start the quiz.")); + return; + } + + let score = 0; + + for (let i = 0; i < questions.length; i++) { + const userAnswer = askQuestion(questions[i], i + 1); + score += checkAnswer(questions[i], userAnswer); + console.log(chalk.magenta(`Current score: ${score}`)); + } + + console.log(chalk.cyan("\nQuiz finished!")); + console.log(chalk.green(`Final score: ${score}/10`)); +} + +startQuiz();