Friday, August 1, 2008

Week 03 HW2 -- WBP

Homework assignment two was much easier than than the first one.
The assigment is two create a javascript Guessing number game.

First you need to start out by creating a random number 1-10.
I still am confused how to create a random number 1-10, but I found some documentation on W3Schools that writes like this

var rndNum = (Math.ceil(Math.random()*10));

This will create a random number 1-10 each time.
I suggest you alert your random number to test your guessing game.

You need to give the user 3 tries to guess the random number, and if they do not guess it correct they fail. But if they guess correctly they win!

I started out by creating a for loop that will occur 3 times.
This is simple by saying that 1 < 3 so keep going

for (i = 1; i <= 3; i++){

This will loop 3 times.
Next you need to get the users input and parse it into a number, like in the last assignment.

var userInp = prompt("Please Guess a Number from 1 - 10", "")
var userNum = parseInt(userInp);
//Notice im parsing variable userInp!

You can basically I copied that from HW1

Now we need to check if the UserNum is equal to our random number

Since our random number is in a variable called rndNum we can create a simple if statement that checks to see if userNum is == to rndNum

If (userInp == rndNum) {
then we go ahead and alert, "Hurray you got the correct number.

}
if else (userInp != rndNum){
then we need to say."You didnt guess the correct number"
}


I ran into a tricky position. I kept repeating the for loop 3 times regardless if they one or loss.

I figured out that if they won, I needed to break the for loop.
I did this by using
break;

The only question is...
Where did I put it?

Also, heres something else I added to my Guessing game.
If you get the message "You didn't guess the correct number"
3 Times, you will get a prompt saying that you have "Lost the Game"
How do you think this was done.

a Hint, it involves an if about my variable i


:D


-Franky

No comments: