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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.env
# System files
.DS_Store
Thumbs.db
Expand Down
36 changes: 34 additions & 2 deletions task-1/Time.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,35 @@
export class Time {
// Your code here
}
#secondsFromMidnight

constructor(hours, minutes , seconds) {

this.#secondsFromMidnight = hours * 3600 + minutes * 60 + seconds
}
getHours() {
return Math.floor(this.#secondsFromMidnight / 3600)
}
getMinutes() {
return Math.floor((this.#secondsFromMidnight % 3600) / 60)
}
getSeconds() {
return this.#secondsFromMidnight % 60
}

addSeconds(seconds) {
this.#secondsFromMidnight += seconds;
this.#secondsFromMidnight = ((this.#secondsFromMidnight % 86400) + 86400) % 86400;
}
addMinutes(minutes) {
this.addSeconds(minutes * 60)
}
addHours(hours) {
this.addSeconds(hours * 3600)
}
toString() {
const hours = String(this.getHours()).padStart(2, '0');
const minutes = String(this.getMinutes()).padStart(2, '0');
const seconds = String(this.getSeconds()).padStart(2, '0');
return `${hours}:${minutes}:${seconds}`;
}
}

5 changes: 5 additions & 0 deletions task-1/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@ import { Time } from './Time.js';

const time = new Time(13, 37, 0);
console.log(time.toString()); // Output: "13:37:00"
console.log(time.getHours()); // Output: 13
time.addMinutes(10);
console.log(time.toString());
time.addSeconds(8000);
console.log(time.toString());
7 changes: 7 additions & 0 deletions task-2/answer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export class Answer {
constructor(answerText, is_Correct) {
this.answerText = answerText;
this.isCorrect = is_Correct;
}

}
80 changes: 79 additions & 1 deletion task-2/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,79 @@
// Write your code here. You may create as many files as you like.
import { fetchQuestions } from "./quiz.js";
import promptSync from "prompt-sync";
import chalk from "chalk";
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

console.log(chalk.magenta("Welcome to the Quiz!"));
const prompt = promptSync();
const category = prompt("Select category to start the quiz: ");

if (!category) {
console.log(
chalk.red(
"Category cannot be empty. Please restart the quiz and enter a valid category.",
),
);
process.exit(1);
}
const questions = await fetchQuestions(category);
if (questions.length === 0) {
console.log(
chalk.red(
"Failed to fetch questions. Please check your API key and try again.",
),
);
process.exit(1);
}

let score = 0;

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

try {
console.log(chalk.magenta(`Loading question ${i + 1}...\n`));
await sleep(1000);
console.log();
console.log(chalk.yellow(question.questionText));
if (question.answers.length > 0) {
question.answers.forEach((answer, index) => {
console.log(`${index + 1}- ${answer.answerText}`);
});
}

const userAnswer = prompt("Your answer is :");
if (
userAnswer.toLowerCase() === "exit" ||
userAnswer.toLowerCase() === "q"
) {
console.log(
chalk.blue(
`Quiz exited! Your final score is: ${score}/${questions.length}`,
),
);
process.exit(0);
}

if (question.isCorrectAnswer(parseInt(userAnswer))) {
console.log(chalk.green("Correct!"));
score++;
} else {
console.log(
chalk.red(
`Wrong! The correct answer is: ${question.answers.findIndex((answer) => answer.isCorrect) + 1} `,
),
);
}
} catch (error) {
console.log(chalk.red.bold(`Error: ${error.message}`));
i--;
}
}

console.log(
chalk.blue(
`Quiz completed! Your final score is: ${score}/${questions.length}`,
),
);
94 changes: 94 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.32.0",
"prompt-sync": "^4.2.0"
}
}
13 changes: 13 additions & 0 deletions task-2/question.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export class Question {
constructor(questionText, answers) {
this.questionText = questionText;
this.answers = answers;
}

isCorrectAnswer(answerIndex) {
if (!answerIndex || answerIndex < 1 || answerIndex > 4) {
throw new Error("Answer index must be between 1 and 4");
}
return this.answers[answerIndex - 1].isCorrect;
}
}
37 changes: 37 additions & 0 deletions task-2/quiz.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import OpenAI from "openai";
import { Question } from "./question.js";
import { Answer } from "./answer.js";
import dotenv from "dotenv";
dotenv.config();
const openai = new OpenAI({
baseURL: "https://models.github.ai/inference/",
apiKey: `${process.env.OPENAI_API_KEY}`,
});

export async function fetchQuestions(category) {
const prompt = `Generate 10 questions , and each questions should include only 4 answer which is only one of them is correct in the ${category} category for each questions from start till end the difficulty of the questions should increase . Reply in a valid and parsable JSON with the following structure:

[{ "questions": "string", "answer_list": [{ "answer": "answer1":is_correct, true/false},{ "answer": "answer2":is_correct, true/false},{ "answer": "answer3":is_correct, true/false},...], }]

do not return anything else besides the JSON and make sure the JSON is valid and parsable dont be markdown.`;
try {
const response = await openai.chat.completions.create({
model: "openai/gpt-4o-mini",
messages: [{ role: "user", content: prompt }],
});

const responseContent = response.choices[0].message.content;

const quiz = JSON.parse(responseContent);
const questions = quiz.questions.map((item) => {
const answers = item.answer_list.map(
(answerItem) => new Answer(answerItem.answer, answerItem.is_correct),
);
return new Question(item.question, answers);
});
return questions;
} catch (error) {
console.error("Error fetching questions:", error);
return [];
}
}