Trying to create a very simple number guessing game as a first project. I want the player to have 5 guesses before they lose.
I'm having difficulty troubleshooting the bugs or coding mistakes.
var num = Math.floor((Math.random(1,10) * 10) +1);
console.log(num); //To check
var counter = 5;
while(counter > 0){
function guess(){
counter = counter-1
var guess = prompt("You've got " + counter + " tries to guess the number.");
if (num == guess){
alert("That's the number!");
}else if (guess != (int){
alert("That's not a number...");
}else{
alert("Nice try");
}
}
}
alert("You lost.");
Working fiddle: http://jsfiddle.net/0skh4t4r/
var num = Math.floor((Math.random(1, 10) * 10) + 1);
console.log(num); //To check
var counter = 5;
function guess() {
var x = prompt("You've got " + counter + " tries to guess the number.");
if (!x) return; // cancel the game
counter--;
if (num === parseInt(x)) {
alert("That's the number!");
return;
} else if (isNaN(x)) {
alert("That's not a number...");
} else {
alert("Nice try");
}
if(counter === 0) {
alert("You lost");
} else {
guess(); // next try
}
}
guess();
Related
I need to create a random number that the user has to guess. When I refresh the page it appears the generate a new random number.
var userPrompt = prompt("Guess a number");
var ran = Math.floor(Math.random() * 10);
function ranNumGame(ran) {
if (userPrompt == ran) {
return "Congrats";
}
else if (ran < userPrompt) {
return "Lower";
}
else if (ran > userPrompt) {
return "Higher";
}
}
console.log(ranNumGame(ran));
var randomNumber = Math.floor(Math.random() * 10);
var userInput = prompt("Pick a number between 0 and 10");
while (userInput != randomNumber) {
alert("nope");
if (userInput < randomNumber) {
userInput = prompt("guess higher");
} else if (userInput > randomNumber) {
userInput = prompt("guess lower");
} else if (userInput == randomNumber) {
alert("congrats");
console.log("congrats");
}
}
some context,
I'm making a game wherein you have to guess a randomly generated number between 1 and 1 million. I'm trying to make it so that if the user inputs something that is not a number, above 1 million, or below zero, it will display an error warning. The last else if statement is supposed to do this, but is not doing so. I've tried several revisions, including individual else if statements for each forbidden case, but they aren't working.
<script type = "text/javascript">
var randomNumber = Math.floor(Math.random() * 1000000 + 1);
var count = 0;
var x = 1000000
console.log(randomNumber)
document.getElementById("submitguess").onclick = function(){
var userGuess = document.getElementById("guessField").value;
console.log(userGuess)
if(userGuess == randomNumber)
{
if (count < 10) {
document.getElementById('higherOrLower').innerHTML = '<div class="powerupstyling"><a class="facethebossstyling">YOU BEAT THE GAME. CONGRATS !</a></div>'
}
else if (count > 10) {
document.getElementById('higherOrLower').innerHTML = "<center>grats you got it in " + count + " tries. Unfortunately, you didn't quite beat the game. Aim for 10 tries or lower.</center>"
}
}
else if (userGuess > randomNumber) {
count++;
document.getElementById('higherOrLower').innerHTML = '<br><div class="powerupstyling"><a class="gostyling">go lower</a></div>'
document.getElementById('countCounter').innerHTML = "<center>tries: " + count + "</center>";
}
else if (userGuess < randomNumber) {
count++;
document.getElementById('higherOrLower').innerHTML = '<br><div class="powerupstyling"><a class="gostyling">go higher</a></div>'
document.getElementById('countCounter').innerHTML = "<center>tries: " + count + "</center>";
}
else if (userGuess >= 1000000 || userGuess =< 0 || userGuess == "") {
alert('input a valid number !')
}
}
</script
You have to write the last else if before you check if the userGuess is higher or lower than the randomNumber.
That's because when userGuess is below 0 or over 1.000.000 it will always be catched by the else ifs that check if the userGuess is lower or higher than randomNumber. Therefore the last else if will never be reached.
The correct order would be:
Check if the user guessed right.
Check if the input is wrong.
Check if the user guessed too high.
Check if the user guessed too low.
But you did:
Check if the user guessed right.
Check if the user guessed too high.
Check if the user guessed too low.
Check if the input is wrong.
Do the following:
<script type = "text/javascript">
var randomNumber = Math.floor(Math.random() * 1000000 + 1);
var count = 0;
var x = 1000000
console.log(randomNumber)
document.getElementById("submitguess").onclick = function(){
var userGuess = document.getElementById("guessField").value;
console.log(userGuess)
if(userGuess == randomNumber)
{
if (count < 10) {
document.getElementById('higherOrLower').innerHTML = '<div class="powerupstyling"><a class="facethebossstyling">YOU BEAT THE GAME. CONGRATS !</a></div>'
}
else if (count > 10) {
document.getElementById('higherOrLower').innerHTML = "<center>grats you got it in " + count + " tries. Unfortunately, you didn't quite beat the game. Aim for 10 tries or lower.</center>"
}
}
else if (userGuess >= 1000000 || userGuess <= 0 || userGuess == "") {
alert('input a valid number !')
}
else if (userGuess > randomNumber) {
count++;
document.getElementById('higherOrLower').innerHTML = '<br><div class="powerupstyling"><a class="gostyling">go lower</a></div>'
document.getElementById('countCounter').innerHTML = "<center>tries: " + count + "</center>";
}
else if (userGuess < randomNumber) {
count++;
document.getElementById('higherOrLower').innerHTML = '<br><div class="powerupstyling"><a class="gostyling">go higher</a></div>'
document.getElementById('countCounter').innerHTML = "<center>tries: " + count + "</center>";
}
}
</script
There are a couple problems with your conditionals.
else if (userGuess > randomNumber)
{ ... }
This will handle all numbers, even those greater than 1,000,000.
else if (userGuess < randomNumber)
{ ... }
This will handle all numbers, even those less than 0.
Therefore, your last conditional will only ever catch a blank input (which would probably be better expressed as a cast to string or bool).
Also, as pointed out in the comments to your question, the correct less than or equal syntax is <=.
You do:
else if (userGuess >= 1000000 || userGuess =< 0 || userGuess == "") {
But it's like this:
else if (userGuess >= 1000000 || userGuess <= 0 || userGuess == "") {
<script type = "text/javascript">
var randomNumber = Math.floor(Math.random() * 1000000 + 1);
var count = 0;
var x = 1000000
console.log(randomNumber)
document.getElementById("submitguess").onclick = function(){
var userGuess = document.getElementById("guessField").value;
console.log(userGuess)
if(userGuess == randomNumber)
{
if (count < 10) {
document.getElementById('higherOrLower').innerHTML = '<div class="powerupstyling"><a class="facethebossstyling">YOU BEAT THE GAME. CONGRATS !</a></div>'
}
else if (count > 10) {
document.getElementById('higherOrLower').innerHTML = "<center>grats you got it in " + count + " tries. Unfortunately, you didn't quite beat the game. Aim for 10 tries or lower.</center>"
}
}
else if (userGuess > randomNumber) {
count++;
document.getElementById('higherOrLower').innerHTML = '<br><div class="powerupstyling"><a class="gostyling">go lower</a></div>'
document.getElementById('countCounter').innerHTML = "<center>tries: " + count + "</center>";
}
else if (userGuess < randomNumber) {
count++;
document.getElementById('higherOrLower').innerHTML = '<br><div class="powerupstyling"><a class="gostyling">go higher</a></div>'
document.getElementById('countCounter').innerHTML = "<center>tries: " + count + "</center>";
}
else if (userGuess >= 1000000 || userGuess <= 0 || userGuess == "") {
alert('input a valid number !')
}
}
</script
This is my first time asking a question here, so pardon if it has an error/is less descriptive.
Actually, I am a beginner in JavaScript, and while making a console guess game, there is just nothing in the output window(it supports alert and prompt boxes). Here's the code:
function runGame() {
Boolean isPlaying = true;
var tries = 3;
var guess = 0;
var randInt = Math.floor(Math.random(10) * 1);
alert("You have 3 chances to guess a number between 1 & 10!");
while (guess != randInt && tries > 0) {
guess = prompt("Enter a guess between 1 & 10: ");
if (guess > randInt) {
tries--;
alert("Too high!");
} else if (guess < randInt) {
tries--;
alert("Too low!");
} else {
alert("Exactly! " + randInt + " it is! You've won!");
}
}
if (tries < 1) {
isPlaying = false;
}
}
while (isPlaying == true) {
runGame();
}
Moved your while isPlaying to the inside while loop. A while loop with a function inside will just call that function over and over again.
Math.random(10) only changes the seed, it does not choose between 1-10.
function runGame() {
var isPlaying = true;
var tries = 3;
var guess = 0;
var randInt = Math.floor(Math.random() * 10);
alert("You have 3 chances to guess a number between 1 & 10!");
while (guess != randInt && tries > 0 && isPlaying) {
guess = prompt("Enter a guess between 1 & 10: ");
if (guess > randInt) {
tries--;
alert("Too high!");
} else if (guess < randInt) {
tries--;
alert("Too low!");
} else {
alert("Exactly! " + randInt + " it is! You've won!");
}
}
if (tries < 1) {
isPlaying = false;
}
}
runGame()
When you run this script it shows the HP for both of the pokemon when you press 1 and click enter it subtracts your attack hit points to the enemies hit points. When you or the ememy hits 0 or less than 0 hit points it is supposed to stop and just show who won in the console log. Instead it takes an extra hit to for it to show the message.
So if you are at -10 hp it takes one more hit.
let firstFight = false;
while (!firstFight) {
let fightOptions = prompt("1. Fight, 2.Items, 3.Potions " + wildPokemon[0].name + ":" + wildPokemon[0].hp + " " + pokeBox[0].name + ":" + pokeBox[0].hp);
if (fightOptions == 1) {
if (!firstFight) {
if (wildPokemon[0].hp <= 0) {
console.log("You have won!");
firstFight = true;
} else {
let attack1 = wildPokemon[0].hp -= pokeBox[0].attack.hp;
console.log(wildPokemon[0].hp);
}
if (pokeBox[0].hp <= 0) {
console.log(wildPokemon[0] + " has killed you");
firstFight = true;
} else {
let attack2 = pokeBox[0].hp -= wildPokemon[0].attack.hp;
console.log(pokeBox[0].hp);
}
}
} else if (fightOptions == 2) {
} else if (fightOptions == 3) {
} else {
}
}
Are there any ways I can make this code more efficient?
you can simply add another if condition to check whether life of the player is still greater then '0' or less then '0' in the same turn like this.
in this way you don't have to go for next turn to check for the players life plus it rids off the extra conditional statements...
if (fightOptions == 1) {
let attack1 = wildPokemon[0].hp -= pokeBox[0].attack.hp;
console.log(wildPokemon[0].hp);
if (wildPokemon[0].hp <= 0) {
console.log("You have won!");
firstFight = true;
}
if (!firstFight){
let attack2 = pokeBox[0].hp -= wildPokemon[0].attack.hp;
console.log(pokeBox[0].hp);
if (pokeBox[0].hp <= 0) {
console.log(wildPokemon[0] + " has killed you");
firstFight = true;
}
}
}
The problem is, the points are getting subtracted after you check if they are equal to or below zero. Here is a way you can check before:
let firstFight = false;
while (!firstFight) {
let fightOptions = prompt("1. Fight, 2.Items, 3.Potions " + wildPokemon[0].name + ":" + wildPokemon[0].hp + " " + pokeBox[0].name + ":" + pokeBox[0].hp);
if (fightOptions == 1) {
wildPokemon[0].hp -= pokeBox[0].attack.hp;
if (wildPokemon[0].hp <= 0) {
console.log("You have won!");
firstFight = true;
} else {
console.log(wildPokemon[0].hp);
}
pokeBox[0].hp -= wildPokemon[0].attack.hp;
if (!firstFight && pokeBox[0].hp <= 0) {
console.log(wildPokemon[0] + " has killed you");
firstFight = true;
} else {
console.log(pokeBox[0].hp);
}
} else if (fightOptions == 2) {
} else if (fightOptions == 3) {
} else {
}
}
While loops stops when the condition is false, in you case, you set it to not false, it is not stopping because you did not explicitly determine it. There are 2 ways you can do.
1st:
while(!firstFight == false)
2nd:
var firstFight = true;
while(firstFight)
then set the firstFight to false inside your if else statements.
My code is not working properly. It is not continuing the loop. It is just showing that your value is low or high and stopping it there. Why doesn't it keep looping?
var target;
var count = 0;
var play;
var game;
function do_this() {
var choose = (Math.floor(Math.random() * 100));
target = choose + 1;
while (true) {
play = prompt("I am thinking of a no. between 1 and 100\n\n" + "enter the no.");
game = parseInt(play);
count = count + 1;
if (isNaN(game)) {
alert("Please enter integer value");
return false;
}
if ((game < 1) || (game > 100)) {
alert("Please enter the value between 1 and 100");
return false;
}
if (game < choose) {
alert("enter higher value");
return false;
}
if (game > choose) {
alert("enter lower value");
return false;
}
alert("You are correct\n\n" + "you took" + count + "to guess the game");
return true;
}
}
do_this()
You are using return false; when the value is higher or lower. Replace that with continue;