I’m trying to use Javascript to make a guess the number game. I need to get a user input and I’m not sure how. Here is the code:
var rdNumber = [31.9375, 382.583, 923.219];
var guessNumber = 0;
maxLivesCount = 10;
livesCounter = 0;
for (let i = 0; i < 10; i++) {
guessNumber =
if (rdNumber[0] === guessNumber) {
console.log("Congragulations you have completed the hardest Guess The Number Game");
livesCounter += 1;
rdNumber.shift();
} else if (livesCounter === maxLivesCount) {
console.log("You have run out of lives, You will never be able to do this challenge again");
}
else {
console.log("Try again");
}
}
Also I know there will be an error with the if command but if you fill the guessNumber variable
I need to find out what to fill the guessNumber with.
I’m going to help you with your problem but give some advice first.
I would highly advise that you take the JavaScript courses here on FreeCodeCamp. They will be a major benefit to you.
If you’re just testing right now, there is a native JS command called prompt that you can use that takes 2 parameters (in order): the message to show to the user (for example, “What is your random number guess?”), and then the default value to place in the box (for your example, probably “0”). prompt returns a string value so you would want to put that into a variable.
And you would want to try to convert that variable into a float value and take care of when it fails to be converted.
Usually, with “prompt until” code you have the prompt inside a while loop. It is how most games work as well, the game loop is pretty much an infinite loop but with some terminating conditions.
You can either use game rules, like a score, attempts, etc. to end the loop. Or you can use a variable that is set inside the conditions (like isGuessing, isRunning, or gameOver) which you keep checking.
It is a little dangerous and you will most likely end up with code in an actual infinite loop while testing it. You can take proactive precautions by giving the loop two conditions one being a max count it can never exceed while (isGuessing && loopProtection < 10).
It is a lot less error-prone if you use an input element for the guessing and avoid the “prompt until” looping.
var guessNumber = 0;
maxLivesCount = 10;
livesCounter = 0;
while (livesCounter < maxLivesCount) {
guessNumberStr = prompt("Enter a number: ");
guessNumber = parseFloat(guessNumberStr);
}
for (let i = 0; i < 10; i++) {
if (rdNumber[0] === guessNumber) {
console.log("Congragulations you have completed the hardest Guess The Number Game");
livesCounter += 1;
rdNumber.shift();
} else if (livesCounter === maxLivesCount) {
console.log("You have run out of lives, You will never be able to do this challenge again");
}
else {
console.log("Try again");
}
}
And here are the test results:
Test 1: Prompt is not Defined
Test 2: Prompt is not Defined
Test 3: Prompt is not Defined
You just have an infinite while loop. The value you get from the prompt never reaches the for loop code because you are just stuck in the while loop code.
If you want the for loop code to use the values inside the while loop code (i.e. the prompt value) you have to have it inside the while loop or put it inside a function you can call inside the while loop.
Yes, prompt is in the browser. You can still get user input in Node. I’m sure there are many ways of doing it including using packages.
You are free to write your code however you want. It was only a suggestion. For the type of loop you need I think the while loop is more appropriate.
With the while loop you do not also need the for loop. You can do it all inside that one loop. The while loop can also make the loop condition more explicit and readable.
Did you read the MDN article I linked to? It is also a number guessing tutorial but it uses the input element. As I said, that is safer and more like how you would do it with a website. You wouldn’t use a prompt as that isn’t a very good user experience.
The prompt command just isn’t great for really any application besides when you’re first learning because it’s not customizable, very limited, and thread blocking (everything else on the page stops until there is input). There are a lot of better ways to get user input, as @lasjorg mentioned, with using inputs. I’ve created custom modals that work in a similar fashion to a prompt but are better for the user because they are highly customizable, and allow background execution to still occur. Keep learning, and happy coding!
I’ve changed my code to a method so here is the new code:
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
var rdNumber = [31.9375, 382.583, 923.219];
maxLivesCount = 10;
livesCounter = 0;
while (maxLivesCount > livesCounter) {
guessNumber = rl.question("Put in a number: ");
if (rdNumber[0] === guessNumber) {
console.log("Congragulations you have completed the hardest Guess The Number Game");
rdNumber.shift();
break;
} else if (livesCounter === maxLivesCount) {
console.log("You have run out of lives, You will never be able to do this challenge again");
break;
}
else {
console.log("Try again");
livesCounter = livesCounter + 1
}
}
It will only return Try Again 10 times. and it doesn’t work otherwise
while (maxLivesCount > livesCounter) {
var guessNumber = readlineSync.question("Put in a number: ");
if (rdNumber[0] == guessNumber) {
console.log(“Congragulations you have completed the hardest Guess The Number Game”);
rdNumber.shift();
} else if (livesCounter === maxLivesCount) {
console.log(“You have run out of lives, You will never be able to do this challenge again”);
}
else {
console.log(“Try again”);
livesCounter = livesCounter + 1
}
}
if (rdNumber[0] == guessNumber) {
console.log(“Congragulations you have completed the hardest Guess The Number Game”);
rdNumber.shift();
if(rdNumber.length===0){
console.log(‘You Guess all numbers!’);
break;
}
I might suggest not using Node.js for this as it doesn’t handle user input the same way and it may become a rabbit hole. Not that learning Node.js is a bad idea but if you are just trying to learn JS the differences (like user input) might not be worth dealing with right now.
Anyway, learning how to do it using HTML elements is more useful. But you can try both.