js code not working for hangman game! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

js code not working for hangman game!

I can't seem to get my code to work properly for my hangman game, specifically the js code. When I run the game, there are supposed to be lines or blanks where the letters should be, but nothing showed. Not to mention, the letter input button doesn't work. If anyone can help, I'll be really grateful! Here's the link to my code: https://code.sololearn.com/WmYHuOYhsQuy/#js

29th Apr 2019, 2:35 AM
Monicaa
8 Answers
+ 2
1. Here is half fix for you: https://code.sololearn.com/WiNseLBayIh4/?ref=app 2. Explanation Below line number is number in my code above. 2.1 At line 42, For window.onload, it is assignment a callback, no need to use keyword function and no need to put () 2.2 At line 27, don't overwrite the correct answer otherwise how you can check it? That's why I add a new declaration at line 23, this variable is for storing the current player answer. 2.3 line 31 has three wrong Not innerhtml is innerHTML Not = should be += Not word should be ans 2.4 Most important wrong is at line 17 and line 49 You should not declare two different variables in local scope, you should declare in global and use them. Read this: https://www.sololearn.com/post/91113/?ref=app 3. After you understand above, makes correction for 2.4, show me your corrections, and then we continue.
29th Apr 2019, 7:40 AM
Gordon
Gordon - avatar
+ 1
excellent job in the toLowerCase() function. yes, if you use guess to check with answer if(guess == answer) then you don't need the variable lettersremaining, but it's up to you. and in my design, the result should keep printing the string guess after each correct guess. but i can see that you want to display two underscore and one space for each unguessed char, so in this case you keep to your ans array and forget about my string guess. Here i have made any edits, i only add 5 comments, numbered. https://code.sololearn.com/W0cAEiIFXjnQ/?ref=app read them one by one, and if you make up the right logic, your game should work. make your edits based on the 5 points i raised, and show me your updated code.
3rd May 2019, 12:18 PM
Gordon
Gordon - avatar
0
Thank you so much, Gordon! I fixed my code according to your instructions and have eliminated the two repetitive variables. Since the link you provided wasn't available, I went to other sources and declared my variable to what I hope is a global variable. However, I still can't seem to get the rest of my code to work. Is there something I'm missing?
2nd May 2019, 11:04 AM
Monicaa
0
Very good. Your letterRemaining is correct now. I'll make edits based on your current version. I'm glad that you learned this important concept about scope. The readings are still worth reading, I'll fetch you the link to the reading materials directly
2nd May 2019, 12:07 PM
Gordon
Gordon - avatar
0
This is a catalog for all posts of everything.js You can use the search function, find the post about closures and open the three links to the reading materials and read them one by one. https://code.sololearn.com/W7P66AVbIyd4/?ref=app
2nd May 2019, 12:11 PM
Gordon
Gordon - avatar
0
This version is fixing 2/3 for you, https://code.sololearn.com/WOK6dr0Pqdjq/?ref=app Read the 16 comments i made for you one by one. Show me you understand them, and then we continue.
2nd May 2019, 12:29 PM
Gordon
Gordon - avatar
0
Hi, Gordon I fixed the things you suggested. However, I don't understand the function of var guess and how it fits into my code. For clarification, is it supposed to replace my lettersremaining variable?
3rd May 2019, 4:42 AM
Monicaa
0
Hi, Gordon I don't think I get it. I've tried altering the code without the use of guess but it won't work, so instead, I decided to use it following your method with as much logic as I can manage and the results remain roughly the same. When the user inputs the correct guess, it either doesn't show or puts in a different letter instead. In addition to that, my variable "tries" decrement by 6 each time instead of 1. I'm sorry for the inconvenience, and I am thankful for your time!
5th May 2019, 2:57 PM
Monicaa