8 Replies - 578 Views - Last Post: 28 February 2014 - 10:10 AM

#1 ojas360  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-February 14

Hangman Code help

Posted 25 February 2014 - 08:07 PM

I m trying to make a hangman type of game...I need some help in a js code
the control is not moving to the verification of string after i enter
 <html>
<head>
<title>Hangman</title>
<link rel="stylesheet" href="hangman.css">
<script type="text/javascript">

function ischar()
{
var a=document.getElementById('guess').value;

if(a.length<=1 && a!=NULL && a!=" " && a!=NaN a>string.CharCode(65) && a<string.CharCode(90) )
{
break;
}
else
{
alert("Please Enter Only One Character");
}
}

function Hangman()
{

var o=document.getElementById('guess');

switch(o)
{
case 'S':
document.getElementByname('txt1').value=('S');
break;

case 's':
document.getElementByname('txt1').value=('S');
break;

case 'P':
document.getElementByname('txt2').value=('P');
break;

case 'p':
document.getElementByname('txt2').value=('P');
break;

case 'R':
document.getElementByname('txt4').value=('R');
break;

case 'r':
document.getElementByname('txt4').value=('R');
break;

case 'K':
document.getElementByname('txt5').value=('K');
break;

case 'k':
document.getElementByname('txt5').value=('K');
break;

default:
alert("Oooops Sorry!!!!Wrong Word!);
}

}




</script>
</head>

<body>

<center> <h1> <font face="Lucida Calligraphy">HANGMAN </font> </h1> </center>
<br> <br> <br> <br>
<input type="text" name="txt1" id="words" readonly>
<input type="text" name="txt2" id="words" readonly>
<input type="text" name="txt3" id="words" readonly value="A">
<input type="text" name="txt4" id="words" readonly>
<input type="text" name="txt5" id="words" readonly>
<br><br><br>
Guess The word:
<input type="textbox" name="txt6" id="guess" onkeypress="ischar()">
<input type="submit" value="Enter" onclick="Hangman()">
</body>
</html>



Is This A Good Question/Topic? 0
  • +

Replies To: Hangman Code help

#2 ojas360  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-February 14

Re: Hangman Code help

Posted 25 February 2014 - 08:16 PM

Made some changes
<html>
<head>
<title>Hangman</title>
<link rel="stylesheet" href="hangman.css">
<script type="text/javascript">

function ischar()
{
var a="document.getElementById('guess').value";

if(a.length<2 && a!=" " && a!=NaN && a>string.CharCode(65) && a<string.CharCode(90))
{
}
else
{
alert("Please Enter Only One Character");
}
}

function Hangman()
{

var o=document.getElementById('guess');

switch(o)
{
case 'S':
document.getElementByname('txt1').value=('S');
break;

case 's':
document.getElementByname('txt1').value=('S');
break;

case 'P':
document.getElementByname('txt2').value=('P');
break;

case 'p':
document.getElementByname('txt2').value=('P');
break;

case 'R':
document.getElementByname('txt4').value=('R');
break;

case 'r':
document.getElementByname('txt4').value=('R');
break;

case 'K':
document.getElementByname('txt5').value=('K');
break;

case 'k':
document.getElementByname('txt5').value=('K');
break;

default:
alert("Oooops Sorry!!!!Wrong Word!");
}

}




</script>
</head>

<body>

<center> <h1> <font face="Lucida Calligraphy">HANGMAN </font> </h1> </center>
<br> <br> <br> <br>
<input type="text" name="txt1" id="words" readonly>
<input type="text" name="txt2" id="words" readonly>
<input type="text" name="txt3" id="words" readonly value="A">
<input type="text" name="txt4" id="words" readonly>
<input type="text" name="txt5" id="words" readonly>
<br><br><br>
Guess The word:
<input type="textbox" name="txt6" id="guess" onkeypress="ischar()">
<input type="submit" value="Enter" onclick="Hangman()">
</body>
</html>



Now whenever i enter a word i get it as wrong word means it goes into else part
Was This Post Helpful? 0
  • +
  • -

#3 FerretHolmes  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 41
  • View blog
  • Posts: 167
  • Joined: 12-November 12

Re: Hangman Code help

Posted 25 February 2014 - 08:30 PM

You have quite a few issues not only with your code but your control flow and logic. You also are using deprecated html markup. I know this isn't very helpful, but there is so much going on that I don't know where to start. I would highly suggest you head over to a site like Codecademy to learn Javascript fundamentals before tackling a project like this.
Was This Post Helpful? 0
  • +
  • -

#4 ojas360  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-February 14

Re: Hangman Code help

Posted 25 February 2014 - 08:39 PM

View PostFerretHolmes, on 25 February 2014 - 08:30 PM, said:

You have quite a few issues not only with your code but your control flow and logic. You also are using deprecated html markup. I know this isn't very helpful, but there is so much going on that I don't know where to start. I would highly suggest you head over to a site like Codecademy to learn Javascript fundamentals before tackling a project like this.


can u tell me whats wrong in it please ..
i have edited in the second post
Was This Post Helpful? 0
  • +
  • -

#5 FerretHolmes  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 41
  • View blog
  • Posts: 167
  • Joined: 12-November 12

Re: Hangman Code help

Posted 25 February 2014 - 08:48 PM

I took the liberty of copy and pasting your code into JSBin. If you look in the Javascript panel and look near the bottom of the column, you will see a red panel that has '17 warnings' text inside. Expand that and fix the errors you see. Once you complete those post your new code and we'll problem solve.

EDIT:
Apologies, I entered your first iteration of the code so ignore my previous statement.

This post has been edited by FerretHolmes: 25 February 2014 - 08:51 PM

Was This Post Helpful? 0
  • +
  • -

#6 ojas360  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-February 14

Re: Hangman Code help

Posted 25 February 2014 - 08:57 PM

Yes i saw many errors in the first one through chrome js console ...But then corrected it ....can u please help me in the second post ...if the logic is right ?

This post has been edited by andrewsw: 26 February 2014 - 11:20 AM
Reason for edit:: Fixed tags

Was This Post Helpful? 0
  • +
  • -

#7 ArtificialSoldier  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 330
  • View blog
  • Posts: 1,206
  • Joined: 15-January 14

Re: Hangman Code help

Posted 26 February 2014 - 10:32 AM

This line is not correct:

var a="document.getElementById('guess').value";


Why do you have that whole thing in quotes? Also, why are you checking if the variable a is NaN? When would it ever be NaN?

var o=document.getElementById('guess');
switch(o)



o is not set to the value of the field, it is set to the actual field. None of the case statements will match the field.

document.getElementByname('txt1').value=('S');



There is no document.getElementByname function.
Was This Post Helpful? 0
  • +
  • -

#8 ojas360  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-February 14

Re: Hangman Code help

Posted 28 February 2014 - 08:27 AM

View PostArtificialSoldier, on 26 February 2014 - 10:32 AM, said:

This line is not correct:

var a="document.getElementById('guess').value";



Why do you have that whole thing in quotes? Also, why are you checking if the variable a is NaN? When would it ever be NaN?
actually i want that entered number should be string only not any integer not even a space so wrote ischar function

View PostArtificialSoldier, on 26 February 2014 - 10:32 AM, said:

var o=document.getElementById('guess');
switch(o)



o is not set to the value of the field, it is set to the actual field. None of the case statements will match the field.

document.getElementByname('txt1').value=('S');



There is no document.getElementByname function.


i didnt understood the o part o is what user enters in the textbox of guess so thats y made cases according to the word SPARK if not the word then it will say wrong word ....
And will replace the byname with byid


Thanks for your help :)
Was This Post Helpful? 0
  • +
  • -

#9 ArtificialSoldier  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 330
  • View blog
  • Posts: 1,206
  • Joined: 15-January 14

Re: Hangman Code help

Posted 28 February 2014 - 10:10 AM

Quote

o is what user enters in the textbox

No it isn't. It is the actual element that they type in, not the value of that element. You want the value, not the actual element.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1