Scoob
Scoob

Reputation: 13

JavaScript beginner: Trouble with strings

At the top of my code I need to ask the users if they wish to play a game. Using the string function to look at the first letter of their answer and make it lower case. If their answer == "y" then play the game. I need help taking the first letter of Yes, and making it lower case.

<HTML>
<HEAD>
</HEAD>
<BODY>

<FORM NAME="testform">
<BR>
<BR>
<BR>
</FORM>

<INPUT id="attempts" TYPE="text" NAME="inputbox" VALUE="" />
<INPUT id="zero" TYPE="button" NAME="resetbox" VALUE="Reset " onclick="reset()" />

<SCRIPT type="text/javascript">

varattempts = 0
x = Math.round((Math.random()*19))+1    
var tip

tip=prompt("Do you want to play a game?")

while(tip == "y")

{

    var Guess;
    document.getElementById('attempts').value = 0;
    do {

    Guess = prompt("Pick a number between 1 and 20","")
    if (Guess === null) break;
    document.getElementById('attempts').value = parseInt(document.getElementById('attempts').value)+1
      } while (Guess!=x);

    if (Guess == x)

    {

    alert("You guessed right!")

    }
}

function reset()

{
    varattempts=0;
    document.getElementById('attempts').value = 'Attempts: 0'; 
}

</SCRIPT>
</BODY>
</HTML>

Upvotes: 0

Views: 69

Answers (1)

Ry-
Ry-

Reputation: 224886

To take the first letter of any string, you can use .charAt(0):

var firstChar = myString.charAt(0);

And to convert it to lowercase:

var firstChar = myString.charAt(0).toLowerCase();

Voilà!

Upvotes: 1

Related Questions