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
43 changes: 42 additions & 1 deletion task-1/Time.js
Original file line number Diff line number Diff line change
@@ -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')}`;
}
}
19 changes: 17 additions & 2 deletions task-1/index.js
Original file line number Diff line number Diff line change
@@ -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
32 changes: 2 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.

153 changes: 152 additions & 1 deletion task-2/index.js
Original file line number Diff line number Diff line change
@@ -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();
}
})();
27 changes: 27 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.

12 changes: 6 additions & 6 deletions task-2/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
Loading