Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 52 additions & 2 deletions task-1/Time.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,53 @@
const SECONDS_IN_HOUR = 3600;
const SECONDS_IN_MINUTE = 60;
const SECONDS_IN_DAY = 86400;

export class Time {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is best to define these constants outside of the class definition so that you can use them in all methods, not just the constructor.

Suggested change
export class Time {
const SECONDS_IN_MINUTE = 60;
const SECONDS_IN_HOUR = SECONDS_IN_MINUTE * 60;
const SECONDS_IN_DAY = SECONDS_IN_HOUR * 24;
export class Time {

// Your code here
}
#secondsFromMidnight = 0;

constructor(hours, minutes, seconds) {
const totalSeconds =
hours * SECONDS_IN_HOUR + minutes * SECONDS_IN_MINUTE + seconds;

if (totalSeconds < 0 || totalSeconds >= SECONDS_IN_DAY) {
throw new Error('Invalid time');
}

this.#secondsFromMidnight = totalSeconds;
}

getHours() {
return Math.floor(this.#secondsFromMidnight / SECONDS_IN_HOUR);
}

getMinutes() {
return Math.floor((this.#secondsFromMidnight % SECONDS_IN_HOUR) / SECONDS_IN_MINUTE);
}

getSeconds() {
return this.#secondsFromMidnight % SECONDS_IN_MINUTE;
}

addSeconds(seconds) {
this.#secondsFromMidnight =
(this.#secondsFromMidnight + seconds) % SECONDS_IN_DAY;
if (this.#secondsFromMidnight < 0) {
this.#secondsFromMidnight += SECONDS_IN_DAY;
}
}

addMinutes(minutes) {
this.addSeconds(minutes * SECONDS_IN_MINUTE);
}

addHours(hours) {
this.addSeconds(hours * SECONDS_IN_HOUR);
}

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}`;
}
}
13 changes: 11 additions & 2 deletions task-1/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { Time } from './Time.js';

const time = new Time(13, 37, 0);
console.log(time.toString()); // Output: "13:37:00"
const myTime = new Time(12, 35, 0);
console.log(myTime.toString()); // 12:35:00
console.log(myTime.getHours()); // 12
console.log(myTime.getMinutes()); // 35
console.log(myTime.getSeconds()); // 0

myTime.addMinutes(25);
console.log(myTime.toString()); // 13:00:00

myTime.addHours(12);
console.log(myTime.toString()); // 01:00:00
30 changes: 0 additions & 30 deletions task-1/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

88 changes: 87 additions & 1 deletion task-2/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,87 @@
// Write your code here. You may create as many files as you like.
import dotenv from 'dotenv';
import OpenAI from 'openai';
import readlineSync from 'readline-sync';
import chalk from 'chalk';

dotenv.config();

const apiConnection = new OpenAI({
baseURL: 'https://models.github.ai/inference/',
apiKey: process.env.OPENAI_API_KEY,
});

async function startQuiz() {
console.log(
chalk.blue.bold('\n=== Welcome to the AI powered quiz game ===\n'),
);
console.log(chalk.yellow('Generating questions from the AI...'));

const response = await apiConnection.chat.completions.create({
model: 'openai/gpt-4o-mini',
messages: [
{
role: 'user',
content: `
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 aiResponseText = response.choices[0].message.content;
const questions = JSON.parse(aiResponseText);

let score = 0;

for (let i = 0; i < questions.length; i++) {
const currentQ = questions[i];

console.log(chalk.cyan(`\nQuestion ${i + 1}: ${currentQ.question}`));

console.log(`1. ${currentQ.options[0]}`);
console.log(`2. ${currentQ.options[1]}`);
console.log(`3. ${currentQ.options[2]}`);
console.log(`4. ${currentQ.options[3]}`);

const userChoice = readlineSync.question('\n write your answer (1-4): ');

if (parseInt(userChoice) === currentQ.correctAnswer) {
console.log(chalk.green('Your answer is correct! +1 point'));
score++;
} else {
console.log(
chalk.red(`✖ Wrong! The correct answer is: ${currentQ.correctAnswer}`),
);
}
}

console.log(chalk.magenta.bold('\n============================'));
console.log(
chalk.white(`Quiz finished! Your score is: `) +
chalk.green.bold(`${score}/10`),
);
console.log(chalk.magenta.bold('============================\n'));
}

startQuiz();
73 changes: 73 additions & 0 deletions task-2/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions task-2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,11 @@
"keywords": [],
"author": "",
"license": "ISC",
"type": "module"
}
"type": "module",
"dependencies": {
"chalk": "^5.6.2",
"dotenv": "^17.3.1",
"openai": "^6.33.0",
"readline-sync": "^1.4.10"
}
}