-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
69 lines (58 loc) · 2.01 KB
/
index.js
File metadata and controls
69 lines (58 loc) · 2.01 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env node
// Importamos el módulo en ES6
import promptSync from 'prompt-sync';
const prompt = promptSync();
// Capturamos la entrada del usuario de forma sincrónica
console.log(`
Welcome to the Number Guessing Game!
I'm thinking of a number between 1 and 100.
Please select the difficulty level:
1. Easy (10 chances)
2. Medium (5 chances)
3. Hard (3 chances)
`);
const nivel = prompt("Enter your choice: ");
console.log("");
let maxAttempts;
switch (nivel) {
case '1':
console.log('Great! You have selected the Easy difficult level');
maxAttempts = 10;
break;
case '2':
console.log('Great! You have selected the Medium difficult level');
maxAttempts = 5;
break;
case '3':
console.log('Great! You have selected the Hard difficult level');
maxAttempts = 3;
break;
default:
console.log('Invalid choice, defaulting to Medium level');
maxAttempts = 5;
}
console.log("Let's start the game");
console.log("");
const numeroAleatorio = Math.floor(Math.random() * 100) + 1;
let attempts = 0;
let guessed = false;
while (attempts < maxAttempts && !guessed) {
const userGuess = parseInt(prompt("Enter your guess: "));
attempts++;
if (isNaN(userGuess)) {
console.log("Please enter a valid number.");
attempts--; // Don't count invalid inputs as attempts
} else if (userGuess === numeroAleatorio) {
console.log(`Congratulations! You guessed the correct number in ${attempts} attempts.`);
guessed = true;
} else if (userGuess > numeroAleatorio) {
console.log(`Incorrect! The number is less than ${userGuess}.`);
console.log(`You have ${maxAttempts - attempts} attempts left.`);
} else {
console.log(`Incorrect! The number is greater than ${userGuess}.`);
console.log(`You have ${maxAttempts - attempts} attempts left.`);
}
}
if (!guessed) {
console.log(`Game over! You've used all ${maxAttempts} attempts. The number was ${numeroAleatorio}.`);
}