Hangman game same letter validation javascript only - javascript

I need help with my hangman game, how do I stop lives going down if players guess repeated letter before, as for now if I run it and player guesses the same letter, it will output that he have already made this guess but the lives is dropping too. Also if players keep input the same correct letter, it will output that he have already made this guesses but it will say he won after inputting the same letter 4-5 times.
1st error: lives dropping even if players use letter that is guessed before
2nd error: players input the same correct letter guessed and game will say he won after inputting 4-5 times
Code
guesses = [];
// Show player their progress | .join returned answer as a string
while (remainingLetters > 0 && lives > 0) {
(answerArray.join(""));
guess = readline.question(name+"'s guess (Enter 9 for lifelines or 0 to pass): ");
guess = guess.toUpperCase();
//if guess is more than 1 letter or no letter, alert player to guess 1 letter only
if (guess.length !== 1) {
console.log("Please enter 1 letter only.");
}
//if valid guess
else {
correctGuess = 0;
for (var j = 0; j < Word.length; j++) {
if (Word[j] == guess) {
answerArray[j] = guess;
remainingLetters--;
correctGuess = 1;
}
}
if (correctGuess == 1) {
console.log("\nGood job! "+guess+" is one of the letters!\n");
console.log(JSON.stringify(answerArray)+"\n");
console.log(JSON.stringify(alphabets)+"\n");
} else {
lives -= 1;
console.log("\nSorry. "+guess+" is not a part of the word.\n");
console.log(JSON.stringify(answerArray)+"\n");
console.log(JSON.stringify(alphabets)+"\n");
console.log("You have "+lives+" lives remaining.\n");
}
if (guesses.includes(guess)) {
console.log("You have already made this guess, please try another letter!\n");
} else {
guesses.push(guess)
}
}
if (remainingLetters == 0) {
console.log("Congratulation! You managed to guess the word!\n");
break;
}
if (lives == 0) {
console.log("Game Over... You failed to guess the word. The word is "+Word+".\n")
}
}

Inside else for valid guess move your entire code inside else of if (guesses.includes(guess)) {. It will solve both of your issues.
// Show player their progress | .join returned answer as a string
while (remainingLetters > 0 && lives > 0) {
(answerArray.join(""));
guess = readline.question(name + "'s guess (Enter 9 for lifelines or 0 to pass): ");
guess = guess.toUpperCase();
//if guess is more than 1 letter or no letter, alert player to guess 1 letter only
if (guess.length !== 1) {
console.log("Please enter 1 letter only.");
}
//if valid guess
else {
if (guesses.includes(guess)) {
console.log("You have already made this guess, please try another letter!\n");
} else {
guesses.push(guess);
correctGuess = 0;
for (var j = 0; j < Word.length; j++) {
if (Word[j] == guess) {
answerArray[j] = guess;
remainingLetters--;
correctGuess = 1;
}
}
if (correctGuess == 1) {
console.log("\nGood job! " + guess + " is one of the letters!\n");
console.log(JSON.stringify(answerArray) + "\n");
console.log(JSON.stringify(alphabets) + "\n");
} else {
lives -= 1;
console.log("\nSorry. " + guess + " is not a part of the word.\n");
console.log(JSON.stringify(answerArray) + "\n");
console.log(JSON.stringify(alphabets) + "\n");
console.log("You have " + lives + " lives remaining.\n");
}
}
}
if (remainingLetters == 0) {
console.log("Congratulation! You managed to guess the word!\n");
break;
}
if (lives == 0) {
console.log("Game Over... You failed to guess the word. The word is " + Word + ".\n")
}
}

Related

How to count number of a pattern in a string of words?

This is the question
Given a dictionary of variable names where each name follows CamelCase notation, print the item containing most words. Please note, abbr, like ID , IP are considered one word. i.e. name IPAddress contains two words, IP and Address
singleWordChecking('HelloWorldWideWeb'); the result count should be 4
singleWordChecking('HelloWWW'); the result count should be 2
below are my function on checking single world
function singleWordChecking(word) {
let singleWordCount = 0;
for (let i = 0; i < word.length; i++) {
if (
word[i].toUpperCase() === word[i] &&
word[i + 1].toLowerCase() === word[i + 1]
) {
singleWordCount += 1;
console.log(singleWordCount);
} else if (
word[i].toUpperCase() === word[i] &&
word[i + 1].toUpperCase() === word[i + 1]
) {
return singleWordCount;
}
}
return singleWordCount;
}
singleWordChecking('HelloWorldWideWeb');
singleWordChecking('HelloWWW');
i try with word[i].toUpperCase() === word[i] for verifying the first letter is capitalized,
then if second letter is lowercase
count + 1
however when the word is 'HelloWWW'
the console shows the error of
Cannot read properties of undefined (reading 'toLowerCase')
at singleWordChecking
How do i fix this so that the edge cases can be considering into the function
The reason you are getting the error is because you are going out of bounds of the array by doing your check for the following letter, you will need to add an extra check for:
i + 1 < world.length
However, there are a couple of other issues. For one, you are returning the count when you should be incrementing the count for all capital abbreviations, also checking for 2 capital letters in a row is insufficient because abbreviations can be any length, therefore a possible solution is:
function singleWordChecking(word) {
let singleWordCount = 0;
let isAbbreviation = false;
for (let i = 0; i < word.length; i++) {
if (word[i].toUpperCase() === word[i]) {
if (i + 1 < word.length && word[i + 1].toLowerCase() === word[i + 1]) {
singleWordCount += 1;
isAbbreviation = false;
continue;
}
if (!isAbbreviation) {
singleWordCount += 1;
}
isAbbreviation = true;
}
}
return singleWordCount;
}

Limit incorrect guess attempts in Hangman Javascript

I'm trying to code a hangman game where a user only has three incorrect guess attempts.
i.e. if they guess the correct letter they continue playing. If they guess incorrectly three times then the game ends.
I've seen some other solutions where functions are used but I'm not at that point in my JS journey yet, so I'm hoping someone can use some basic syntax.
I'm guessing it's a general no-no to further nest 'if', 'else if' and 'else' statements in an 'else' statement. I've also tried to add another 'for loop' in the 'while loop' but this just breaks as well.
As the code stands now, when I enter a correct OR incorrect letter, the game terminates and shows me the final alert. Could someone pls explain to me what's happening?
Thanks :)
var words = [
"javascript",
"monkey",
"amazing",
"pancake",
"discipline",
"integrity",
"enjoy"
]
var word = words[Math.floor(Math.random() * words.length)];
console.log(word);
var answerArray = [];
for (var i = 0; i < word.length; i++) {
answerArray[i] = "_";
}
console.log(answerArray);
var remainingLetters = word.length;
console.log(remainingLetters);
var maxTries = 3;
var guessedWrong = [];
while (remainingLetters > 0 && guessedWords.length < maxTries) {
alert(answerArray.join(" "));
var guess = prompt("Guess a letter, or click Cancel to stop playing.");
guess = guess.toLowerCase();
if (guess === null) {
break;
} else if (guess.length !== 1) {
alert("please enter a single letter.");
} else {
for (var j = 0; j < word.length; j++) {
if (guess === word[j]) {
answerArray[j] = guess;
remainingLetters--;
// if incorrect letter guessed, then push to guessedWrong array;
// have tried another 'if', 'else if', and 'else' here but luck;
} else if (guess !== word[j]) {
guessedWrong.push(guess);
}
}
}
}
alert(answerArray.join(" "));
alert("Good job! The answer was " + word);
The problem was that the if clause that was incrementing guessedWrong was being called for each character in the word, and regardless of the input character. That means that at the end of one while iteration, guessedWrong.length would've been close to word.length, ending the game.
I've added a check to customize the end game message depending on whether the word was found or not
also wrapped everything in a function to demonstrate how to use functions.
have added comment blocks to detail the changes, most of them are towards the end of the code
I hope this helps
This implementation is great and you're doing a great job. Good luck on your journey!
function startGame() {
var words = [
"javascript",
"monkey",
"amazing",
"pancake",
"discipline",
"integrity",
"enjoy"
]
var word = words[Math.floor(Math.random() * words.length)];
console.log(word);
var answerArray = [];
for (var i = 0; i < word.length; i++) {
answerArray[i] = "_";
}
console.log(answerArray);
var remainingLetters = word.length;
console.log(remainingLetters);
var maxTries = 3;
var guessedWrong = [];
while (remainingLetters > 0 && guessedWrong.length < maxTries) {
alert(answerArray.join(" "));
var guess = prompt("Guess a letter, or click Cancel to stop playing.");
guess = guess.toLowerCase();
if (guess === null) {
break;
} else if (guess.length !== 1) {
alert("please enter a single letter.");
} else {
for (var j = 0; j < word.length; j++) {
if (guess === word[j]) {
answerArray[j] = guess;
remainingLetters--;
// if incorrect letter guessed, then push to guessedWrong array;
// have tried another 'if', 'else if', and 'else' here but luck;
}
// if we add the if check here inside the for loop, then with each iteration we wrongfully increment
// the guessedWrong array, and game will be over on next while iteration.
// so we've moved it outside the for loop
}
// String.indexOf() returns -1 if a specific character is not found inside that string. otherwise it will return
// the position at which that character is first found inside the string. we can also use lastIndexOf() which checks
// for the same thing, except it starts looking from the end of the array.
if (word.indexOf(guess) === -1) {
guessedWrong.push(guess);
}
}
}
// here we've customized the alert depending on whether the game was won or not
if (guessedWrong.length == maxTries) {
alert('Sorry, you have exhausted all attempts. The correct answer was ' + word);
} else {
alert("Good job! The answer was " + word);
}
// we've wrapped the whole thing in a function so that we can call it and start the game again.
if (confirm('Do you want to play again?')) {
startGame()
}
}
// since it's a function, we have to call it to start the game
startGame()
I slightly modified your implementation and it seems to work well
take a look
var words = [
"javascript",
"monkey",
"amazing",
"pancake",
"discipline",
"integrity",
"enjoy"
]
const word = words[Math.floor(Math.random() * words.length)];
console.log(word);
let answerArray = word.split('').map(_ => '_');
const letters = new Set(word.split(''))
console.log(letters);
let errors = 0;
const maxRetry = 3
while (true) {
alert(answerArray.join(" "));
let guess = prompt("Guess a letter, or click Cancel to stop playing.");
if (guess === null) {
continue;
} else if (guess.length !== 1) {
alert("please enter a single letter.");
continue;
}
guess = guess.toLowerCase().trim()
if (!letters.has(guess)) {
errors++
if (errors === maxRetry) {
alert('you lose the answer was ' + word)
break;
}
}
word.split('').forEach((l, i) => {
if (l === guess) {
answerArray[i] = l
letters.delete(l)
}
})
if (letters.size === 0) {
alert(answerArray.join(" "));
alert("Good job! The answer was " + word);
break;
}
}

Trying to iterate over if..else but numbers aren't adding up

I'm trying to iterate over this if else statement as many times as someone wants. If the score = counter wins should get +1 each time, same with losses. However every time it goes through this wins and losses will only stay at 1.
var wins1 = 0;
var losses1 = 0;
if(counter == numberToguess)
{
counter = 0
console.log("you win")
randfunction()
randNum();
$(".scoreDiv").text(numberToguess)
losses1 += 1
console.log(wins1)
} else if(counter > numberToguess)
{
counter = 0
console.log("you lose")
randfunction()
randNum()
$(".scoreDiv").text(numberToguess)
losses1 += 1
console.log(losses1)
}
It's hard to tell from the context, but are you declaring win1 and losses1 at the top of your loop? If so then they are going to reset to 0 every time the loop runs, hence the final result of 1.
You have to post what you are using to populate numberToguess so that we can help you finish the script. Ill assume your using a prompt window:
var possibilities = 3;
var wins = 0;
var losses = 0;
var answer = "";
while(answer = prompt("guess a number")) {
var randomNumber = Math.floor(Math.random() * possibilities) + 1;
if(answer == randomNumber) {
wins++;
console.log("You Win");
} else {
losses++;
console.log("You Lose");
}
console.log("wins: " + wins + " - losses: " + losses);
}

number of chances in hangman game in javascript

I just developed the hangman game in javascript. I want to add the number of chances the player is left with after giving a wrong answer. Let's say I want to give him a maximum of 7 chances. I also want it to be displayed in the prompt.
var words = ["THE GRAND BUDAPEST HOTEL","MATRIX RELOADED","GLADIATOR","BEN HUR","SAVING PRIVATE RYAN"];
var word = words[Math.floor(Math.random() * words.length)];
var answerArray = [];
for (var i = 0; i < word.length; i++)
{
answerArray[i] = "_";
if (word[i] === " ")
{
answerArray[i] = " ";
}
}
var remainingLetters = word.length;
while( remainingLetters > 0)
{
alert(answerArray.join(" "));
var guess = prompt("Guess a letter or click cancel to stop playing.");
guess = guess.toUpperCase();
if ( guess === null)
{
break;
}
else if(guess.length !== 1)
{
alert("Please enter a single letter.");
}
else
{
for (var j = 0; j < word.length; j++)
{
if (word[j] === guess)
{
answerArray[j] = guess;
remainingLetters--;
}
}
}
}
alert(answerArray.join(" "));
alert("Good job! The answer was " + word);
Add a tries variable at the beginning, initialized with max chances.
Add a condition to your while loop, tries must be > 0
Add it to the prompt :
"Guess a letter or click cancel to stop playing." + tries + " remaining"
If user don't guess a letter (figure this out yourself), decrement tries
At the end, check if user has succeeded (for example check if tries == 0)
If not, print that : (from http://www.berkeleyinternet.com/perl/node30.html)
var hangString = "";
hangString += " ________ \n";
hangString += "| | \n";
hangString += "| 0 \n";
hangString += "| /|\\ \n";
hangString += "| / \\ \n";
hangString += "| \n";

Why does my program skip the game part?

i wrote a very basic hangman game but it skips the actual hangman part. It will show the alert for how long the word is you press ok then it show the finish message no game part and i can't find out why
please tell me what is wrong
Hangman
<body>
<h1>Hangman!</h1>
<script>
var words = [
"javascript",
"monkey",
"amazing",
"pancake",
];
var word = words[Math.floor(Math.random() * words.length)];
var answerArray = [];
for (var i = 0; i < word.length; i++) {
answerArray[i] = "_"
}
var remainingLetters = word.length
while (remainingLetters < 0) {
alert(answerArray.join(" "));
var guess = prompt("Guess a letter, or click Cancel to stop playing>");
if (guess === null) {
break;
} else if (guess.length !== 1) {
alert("Please enter a single letter.");
} else {
//update the game state with a guess
for (var j = 0; j < word.length; j++) {
if (word[j] === guess) {
answerArray[j] = guess;
remainingLetters--;
}
}
}
}
alert(answerArray.join(" "));
alert("Good Job! The answer was " + word);
</script>
</body>
</html>
while (remainingLetters < 0) { should be while (remainingLetters > 0) {.
Also, as Pluto mentioned it (good catch!), you can cheat by entering the same letter over and over. To solve that, you could store the guessed letters in a string and check if it was guessed before (and not increment the score).
Another adjustment, if the user hits cancel (wants to quit), I added an if statement at the end so that no more alerts are displayed.
var words = ["javascript", "monkey", "amazing", "pancake"];
var word = words[Math.floor(Math.random() * words.length)];
var answerArray = [];
// Storing the letters already guessed
var guessedLetters = "";
for (var i = 0; i < word.length; i++) {
answerArray[i] = "_"
}
var remainingLetters = word.length;
while (remainingLetters > 0) {
alert(answerArray.join(" "));
var guess = prompt("Guess a letter, or click Cancel to stop playing>");
if (guess === null) {
break;
} else if (guess.length !== 1) {
alert("Please enter a single letter.");
} else {
// if the letter was already guessed
if (guessedLetters.indexOf(guess) > -1) {
// skip
continue;
}
//update the game state with a guess
for (var j = 0; j < word.length; j++) {
if (word[j] === guess) {
answerArray[j] = guess;
// add the letter to the guessed letters
guessedLetters += guess;
remainingLetters--;
}
}
}
}
// if there are no remaining letters (if the user cancelled,
// no need to show these).
if( !remainingLetters) {
alert(answerArray.join(" "));
alert("Good Job! The answer was " + word);
}
JS Fiddle Demo

Categories

Resources