diff --git a/task-1/Time.js b/task-1/Time.js index 97ca7e2..25d04c0 100644 --- a/task-1/Time.js +++ b/task-1/Time.js @@ -1,3 +1,58 @@ +let result = 0; + 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.hours = hours; + this.minutes = minutes; + this.seconds = seconds; + } + + getHours() { + return this.hours; + } + + getMinutes() { + return this.minutes; + } + + getSeconds() { + return this.seconds; + } + + #normilized() { + if (this.#secondsFromMidnight < 0) { + this.#secondsFromMidnight = + 86400 - Math.abs(this.#secondsFromMidnight % 86400); + } + if (this.#secondsFromMidnight >= 86400) { + this.#secondsFromMidnight = this.#secondsFromMidnight % 86400; + } + this.hours = Math.floor(this.#secondsFromMidnight / 3600); + this.minutes = Math.floor((this.#secondsFromMidnight % 3600) / 60); + this.seconds = + this.#secondsFromMidnight - this.hours * 3600 - this.minutes * 60; + return this.#secondsFromMidnight; + } + + addSeconds(sec) { + this.#secondsFromMidnight = this.#secondsFromMidnight + sec; + return this.#normilized(); + } + + addMinutes(min) { + this.#secondsFromMidnight = this.#secondsFromMidnight + min * 60; + return this.#normilized(); + } + + addHours(hrs) { + this.#secondsFromMidnight = this.#secondsFromMidnight + hrs * 3600; + return this.#normilized(); + } + + toString() { + return `${String(this.hours).padStart(2, "0")}:${String(this.minutes).padStart(2, "0")}:${String(this.seconds).padStart(2, "0")}`; + // return `${("0" + this.hours).slice(-2)}:${("0" + this.minutes).slice(-2)}:${("0" + this.seconds).slice(-2)} + } +} diff --git a/task-1/index.js b/task-1/index.js index cfa7c93..53e5b7e 100644 --- a/task-1/index.js +++ b/task-1/index.js @@ -1,4 +1,5 @@ import { Time } from './Time.js'; -const time = new Time(13, 37, 0); -console.log(time.toString()); // Output: "13:37:00" +const time = new Time(23, 59, 50); +// console.log(time.toString()); // Output: "13:37:00" +console.log(time.addSeconds(20)) diff --git a/task-1/package-lock.json b/task-1/package-lock.json index aba2728..26b46fa 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": [ diff --git a/task-2/basePrompt.js b/task-2/basePrompt.js new file mode 100644 index 0000000..94d9390 --- /dev/null +++ b/task-2/basePrompt.js @@ -0,0 +1,14 @@ +export const BASE_PROMPT = `You are a quiz master specializing in Ancient Egypt. +Generate exactly 1 unexpected quiz question about Ancient Egypt. + +Return ONLY a valid JSON with no markdown, no explanation, no code fences. +Each element must have this exact shape: +{ + "question": "string", + "answers": ["string", "string", "string", "string"], + "correct": 1 +} +"correct" is the 1-based index of the right answer (1–4). + +Vary difficulty: questions 1-3 easy, 4-7 medium, 8-10 hard. +Cover a mix of subtopics: pharaohs, gods, monuments, daily life, writing, history.` \ No newline at end of file diff --git a/task-2/index.js b/task-2/index.js index 02acffa..cf3dad0 100644 --- a/task-2/index.js +++ b/task-2/index.js @@ -1 +1,71 @@ -// Write your code here. You may create as many files as you like. +import "dotenv/config"; +import { OpenAI } from "openai/client.js"; +import promptSync from "prompt-sync"; +import chalk from "chalk"; +import { BASE_PROMPT } from "./basePrompt.js"; +import {userAnswer, isValidAnswer} from "./utils.js" + +const prompt = promptSync(); + +const openai = new OpenAI({ + baseURL: "https://models.github.ai/inference/", + apiKey: process.env.API_KEY, +}); + +async function app() { + try { + console.log("Welcome to the quiz ANCIENT EGYPT! Ready to begin?"); + prompt("Press enter to start the game"); + + const score = { + totalQuestions: 10, + currentQuestion: 1, + correctAnswers: 0, + wrongAnswers: 0, + }; + + while (score.currentQuestion <= score.totalQuestions) { + console.log(`Loading question ${score.currentQuestion}...`); + const response = await openai.chat.completions.create({ + model: "openai/gpt-4o-mini", + response_format: { type: "json_object" }, + messages: [{ role: "user", content: BASE_PROMPT }], + }); + + const responseContent = response.choices[0].message.content; + const result = JSON.parse(responseContent); + + console.log(` +${chalk.blue(result.question)} +${result.answers.map((answer, i) => `${i + 1}. ${answer}`).join("\n")} +`); + + let answer = userAnswer(); + + while (true) { + if (isValidAnswer(answer)) { + break; + } else { + console.error("Please enter an integer number between 1 and 4: "); + answer = userAnswer(); + } + } + + if (result.correct === answer) { + console.log(chalk.green("Correct! Well done.")); + score.correctAnswers += 1; + } else { + console.log(chalk.red(`Wrong! The correct answer was: ${result.correct}`)); + score.wrongAnswers += 1; + } + score.currentQuestion += 1; + } + + console.log( + chalk.yellow(`Quiz finished! Your final score is ${score.correctAnswers}/${score.totalQuestions}`), + ); + } catch (error) { + console.error(error.message); + } +} +app(); \ 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..5f7dff4 --- /dev/null +++ b/task-2/package-lock.json @@ -0,0 +1,104 @@ +{ + "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", + "dotenv": "^17.3.1", + "openai": "^6.33.0", + "prompt-sync": "^4.2.0", + "readline-sync": "^1.4.10" + } + }, + "node_modules/ansi-regex": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-4.1.1.tgz", + "integrity": "sha512-ILlv4k/3f6vfQ4OoP2AGvirOktlQ98ZEL1k9FaQjxa3L1abBgbuTDAdPOpvbGncC0BTVQrl+OM8xZGK6tWXt7g==", + "license": "MIT", + "engines": { + "node": ">=6" + } + }, + "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/dotenv": { + "version": "17.3.1", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-17.3.1.tgz", + "integrity": "sha512-IO8C/dzEb6O3F9/twg6ZLXz164a2fhTnEWb95H23Dm4OuN+92NmEAlTrupP9VW6Jm3sO26tQlqyvyi4CsnY9GA==", + "license": "BSD-2-Clause", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://dotenvx.com" + } + }, + "node_modules/openai": { + "version": "6.33.0", + "resolved": "https://registry.npmjs.org/openai/-/openai-6.33.0.tgz", + "integrity": "sha512-xAYN1W3YsDXJWA5F277135YfkEk6H7D3D6vWwRhJ3OEkzRgcyK8z/P5P9Gyi/wB4N8kK9kM5ZjprfvyHagKmpw==", + "license": "Apache-2.0", + "bin": { + "openai": "bin/cli" + }, + "peerDependencies": { + "ws": "^8.18.0", + "zod": "^3.25 || ^4.0" + }, + "peerDependenciesMeta": { + "ws": { + "optional": true + }, + "zod": { + "optional": true + } + } + }, + "node_modules/prompt-sync": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/prompt-sync/-/prompt-sync-4.2.0.tgz", + "integrity": "sha512-BuEzzc5zptP5LsgV5MZETjDaKSWfchl5U9Luiu8SKp7iZWD5tZalOxvNcZRwv+d2phNFr8xlbxmFNcRKfJOzJw==", + "license": "MIT", + "dependencies": { + "strip-ansi": "^5.0.0" + } + }, + "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" + } + }, + "node_modules/strip-ansi": { + "version": "5.2.0", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-5.2.0.tgz", + "integrity": "sha512-DuRs1gKbBqsMKIZlrffwlug8MHkcnpjs5VPmL1PAh+mA30U0DTotfDZ0d2UUsXpPmPmMMJ6W773MaA3J+lbiWA==", + "license": "MIT", + "dependencies": { + "ansi-regex": "^4.1.0" + }, + "engines": { + "node": ">=6" + } + } + } +} diff --git a/task-2/package.json b/task-2/package.json index a992f45..fc79318 100644 --- a/task-2/package.json +++ b/task-2/package.json @@ -4,10 +4,18 @@ "description": "", "main": "index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "echo \"Error: no test specified\" && exit 1", + "start": "node index.js" }, "keywords": [], "author": "", "license": "ISC", - "type": "module" -} \ No newline at end of file + "type": "module", + "dependencies": { + "chalk": "^5.6.2", + "dotenv": "^17.3.1", + "openai": "^6.33.0", + "prompt-sync": "^4.2.0", + "readline-sync": "^1.4.10" + } +} diff --git a/task-2/utils.js b/task-2/utils.js new file mode 100644 index 0000000..6345df5 --- /dev/null +++ b/task-2/utils.js @@ -0,0 +1,8 @@ +import promptSync from "prompt-sync"; + +const prompt = promptSync(); + +export const userAnswer = () => Number((prompt("Your answer (1-4): ") || "").trim()); + +export const isValidAnswer = (userAnswer) => + userAnswer >= 1 && userAnswer <= 4 && Number.isInteger(userAnswer); \ No newline at end of file