-
Notifications
You must be signed in to change notification settings - Fork 0
/
ngt.ts
44 lines (36 loc) · 1.32 KB
/
ngt.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import promptSync from 'prompt-sync';
const prompt = promptSync();
class NumberGuessingGame {
private targetNumber: number;
constructor() {
this.targetNumber = this.generateRandomNumber(1, 100);
}
private generateRandomNumber(min: number, max: number): number {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
private getGuess(): number {
const guess = parseInt(prompt('Enter your guess (between 1 and 100): '), 10);
if (isNaN(guess) || guess < 1 || guess > 100) {
console.log('Invalid input. Please enter a number between 1 and 100.');
return this.getGuess();
}
return guess;
}
public play(): void {
console.log('Welcome to the Number Guessing Game!');
let guessedCorrectly = false;
while (!guessedCorrectly) {
const guess = this.getGuess();
if (guess < this.targetNumber) {
console.log('Too low! Try again.');
} else if (guess > this.targetNumber) {
console.log('Too high! Try again.');
} else {
console.log(`Congratulations! You guessed the correct number: ${this.targetNumber}`);
guessedCorrectly = true;
}
}
}
}
const game = new NumberGuessingGame();
game.play();