-
Notifications
You must be signed in to change notification settings - Fork 18
Salem Ba-Rabuod #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Barboud
wants to merge
3
commits into
HackYourAssignment:main
Choose a base branch
from
Barboud:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Salem Ba-Rabuod #19
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 { | ||
| // 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}`; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.