From 29b84acd8c326afbc2130de7f0311f709d187057 Mon Sep 17 00:00:00 2001 From: Mareh Date: Wed, 25 Mar 2026 23:52:21 +0100 Subject: [PATCH] week 11 --- .gitignore | 1 + task-1/Time.js | 72 ++++++++++++++++++- task-1/package-lock.json | 32 +-------- task-2/index.js | 151 ++++++++++++++++++++++++++++++++++++++- 4 files changed, 223 insertions(+), 33 deletions(-) diff --git a/.gitignore b/.gitignore index 2b76d7c..d49e216 100644 --- a/.gitignore +++ b/.gitignore @@ -88,6 +88,7 @@ web_modules/ .env.* !.env.example + # parcel-bundler cache (https://parceljs.org/) .cache .parcel-cache diff --git a/task-1/Time.js b/task-1/Time.js index 97ca7e2..b537b4f 100644 --- a/task-1/Time.js +++ b/task-1/Time.js @@ -1,3 +1,71 @@ export class Time { - // Your code here -} \ No newline at end of file + #secondsFromMidnight; + constructor(hours, minutes, seconds) { + const totalSeconds = hours * 3600 + minutes * 60 + seconds; + if (totalSeconds < 0 || totalSeconds >= 86400) { + throw new Error("Invalid"); + } + this.#secondsFromMidnight = totalSeconds; + } + getHours() { + let resultHours = Math.floor(this.#secondsFromMidnight / 3600); + return resultHours; + } + getMinutes() { + let resultMinutes = Math.floor((this.#secondsFromMidnight % 3600) / 60); + return resultMinutes; + } + getSeconds() { + let resultSeconds = Math.floor(this.#secondsFromMidnight % 60); + return resultSeconds; + } + addSeconds(seconds) { + let currentTime = this.#secondsFromMidnight; + let newTotalSeconds = currentTime + seconds; + newTotalSeconds= newTotalSeconds % 86400; + if (newTotalSeconds < 0) { + newTotalSeconds += 86400; + } + this.#secondsFromMidnight= newTotalSeconds; + } + addMinutes(minutes) { + let currentTime = this.#secondsFromMidnight; + let newTotalMinutes = currentTime + (minutes * 60); + newTotalMinutes = newTotalMinutes % 86400; + if (newTotalMinutes < 0){ + newTotalMinutes += 86400; + } + this.#secondsFromMidnight = newTotalMinutes; + } + addHours(hours) { + let currentTime = this.#secondsFromMidnight; + let newTotalHours = currentTime + (hours * 3600); + newTotalHours = newTotalHours % 86400; + if(newTotalHours < 0){ + newTotalHours += 86400; + } + this.#secondsFromMidnight= newTotalHours; + } + toString() { + let seconds = this.getSeconds() + let minutes = this.getMinutes() + let hours = this.getHours() + seconds = String(seconds).padStart(2, '0'); + minutes = String(minutes).padStart(2, '0'); + hours = String(hours).padStart(2, '0'); + let combine = [hours, minutes, seconds].join(':'); + return combine; + } +} + +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 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..a3f53a8 100644 --- a/task-2/index.js +++ b/task-2/index.js @@ -1 +1,150 @@ -// Write your code here. You may create as many files as you like. +import OpenAI from "openai"; +import chalk from "chalk"; +import dotenv from "dotenv"; +dotenv.config(); +import promptSync from "prompt-sync"; +const prompt = promptSync(); + +const openai = new OpenAI({ + baseURL: "https://models.github.ai/inference/", + apiKey: process.env.GITHUB_TOKEN, + //apiKey: process.env.OPENAI_API_KEY, +}); + +let countScore = 0; +// Here I was trying to understand how it works without Ai. +/*const question1 = { + text: "What is the largest planet in our solar system?", + answers: ["Earth", "Jupiter", "Mars", "Saturn"], + correct: 2, +}; +const question2 = { + text: "Who wrote the play Romeo and Juliet?", + answers: [ + "William Shakespeare", + "Charles Dickens", + "Jane Austen", + "Mark Twain", + ], + correct: 1, +}; + +const question3 = { + text: "What is the chemical symbol for gold?", + answers: ["Au", "Ag", "Go", "Ga"], + correct: 1, +}; + +const question4 = { + text: "What is the capital of Japan?", + answers: ["Beijing", "Seoul", "Tokyo", "Bangkok"], + correct: 3, +}; +const question5 = { + text: "Which organ in the human body produces insulin?", + answers: ["Liver", "Pancreas", "Kidney", "Heart"], + correct: 2, +}; +const question6 = { + text: "What is the hardest natural substance on Earth?", + answers: ["Diamond", "Quartz", "Iron", "Topaz"], + correct: 1, +}; + +const question7 = { + text: "Who painted the Mona Lisa?", + answers: [ + "Vincent van Gogh", + "Leonardo da Vinci", + "Pablo Picasso", + "Michelangelo", + ], + correct: 2, +}; +const question8 = { + text: "What gas do plants absorb from the atmosphere during photosynthesis?", + answers: ["Oxygen", "Carbon dioxide", "Nitrogen", "Hydrogen"], + correct: 2, +}; +const question9 = { + text: "How many continents are there on Earth?", + answers: ["5", "6", "7", "8"], + correct: 3, +}; + +const question10 = { + text: "Which element is most abundant in the Earth’s crust?", + answers: ["Oxygen", "Silicon", "Aluminum", "Iron"], + correct: 1, +};*/ +/*const questions = [ + question1, + question2, + question3, + question4, + question5, + question6, + question7, + question8, + question9, + question10, +];*/ +const topic = prompt("Choose a topic (e.g. sports, science): "); +const response = await openai.chat.completions.create({ + model: "openai/gpt-4o-mini", + messages: [ + { + role: "user", + content: `Generate 10 quiz questions about ${topic}. + +Difficulty: +- Questions 1–3: easy +- Questions 4–7: medium +- Questions 8–10: hard + +Return ONLY valid JSON (no text, no explanation). + +Format: +[ + { + "text": "Question here", + "answers": ["A", "B", "C", "D"], + "correct": 1 + } +] + +Rules: +- "answers" must have exactly 4 items +- "correct" must be a number from 1 to 4 +- Do not include numbering like "1." inside answers +`, + }, + ], +}); + +const responseContent = response.choices[0].message.content; +const questions = JSON.parse(responseContent); +questions.forEach((question) => { + console.log(question.text); + question.answers.forEach((answer, index) => { + console.log(index + 1 + ". " + answer); + }); + let userAnswer = prompt("Your answer: "); + userAnswer = Number(userAnswer); + while (userAnswer < 1 || userAnswer > 4 || isNaN(userAnswer)) { + userAnswer = Number(prompt("Please enter a number between 1 and 4: ")); + } + if (userAnswer === question.correct) { + countScore += 1; + console.log(chalk.green("Well Done!")); + } else { + //countScore -= 1; + console.log( + chalk.red( + `Wrong Answer. Correct answer is: ${question.correct}`, + ), + ); + } + console.log("\n"); +}); +console.log(chalk.yellow("Your score: " + countScore + "/10"));