dcsimg
www.webdeveloper.com
Results 1 to 3 of 3

Thread: Code won't return anything

  1. #1
    Join Date
    Mar 2014
    Posts
    6

    Code won't return anything

    <html>

    <head>
    <script src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
    <script>
    function play(userChoice) {
    $("#game").append("poop<br>");
    var computerChoice = Math.random();
    if (computerChoice < 0.34) {
    computerChoice = "rock";
    } else if (computerChoice <= 0.67) {
    computerChoice = "paper";
    } else {
    computerChoice = "scissors";
    }

    $("#game").append(compare(userChoice, computerChoice)+"<br>");
    };
    var compare = function (choice1, choice2) {
    choice1 = choice1.toLowerCase();
    choice2 = choice2.toLowerCase();
    if (choice1 === choice2) {
    return "Stop picking what the computer picked!";
    } else if (choice1 === "rock") {
    if (choice2 === "scissors") {
    return "You win!";
    } else
    return "You lost to a computer";
    }

    if (choice1 === choice2) {
    return "Stop picking what the computer picked!";
    } else if (choice1 === "paper") {
    if (choice2 === "rock") {
    return "You win!";
    } else
    return "You lost to a computer";
    }
    if (choice1 === choice2) {
    return(choice1 === "scissors") {
    if (choice2 === "rock") {
    return "You win!";
    } else
    return "You lost to a computer";
    }

    return "Why did you have to test my game like that?!?! Next time choose rock, paper, or scissors!!"
    };
    </script>
    </head>

    <body>
    <button onclick="play('rock')">Rock</button>
    <button onclick="play('paper')">Paper</button>
    <button onclick="play('scissors')">Scissors</button>
    <span id="game"></span>
    </body>

    </html>

  2. #2
    Join Date
    Mar 2005
    Location
    Behind you...
    Posts
    1,033
    I really can't stress enough just how important it can be to put your code inside of a CODE bbcode. Trying to read and/or copy what you paste directly into a post becomes incredibly sloppy and unpleasant.

    Moving on, your code suffers from syntax errors. Pressing F12 in any major browser will allow you to view the errors (along with the file and line number) to debug issues such as this. You were missing brackets ({ and }) as well as some simply malformed lines of code (return(choice1 === "scissors") {). After all is said and done, you would end up with something like this:
    HTML Code:
    <!doctype html>
    <html>
    <head>
    	<script src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
    	<script>
    	function play(userChoice) {
    		$("#game").append("poop<br>");
    		var computerChoice = Math.random();
    		if(computerChoice < 0.34) {
    			computerChoice = "rock";
    		} else if(computerChoice <= 0.67) {
    			computerChoice = "paper";
    		} else {
    			computerChoice = "scissors";
    		}
    
    		$("#game").append(compare(userChoice, computerChoice)+"<br>");
    	}
    
    	var compare = function(choice1, choice2) {
    		choice1 = choice1.toLowerCase();
    		choice2 = choice2.toLowerCase();
    		if(choice1 === choice2) {
    			return "Stop picking what the computer picked!";
    		} else if(choice1 === "rock") {
    			if (choice2 === "scissors") {
    				return "You win!";
    			} else {
    				return "You lost to a computer";
    			}
    		} else if(choice1 === "paper") {
    			if (choice2 === "rock") {
    				return "You win!";
    			} else {
    				return "You lost to a computer";
    			}
    		} else if(choice1 === "scissors") {
    			if (choice2 === "rock") {
    				return "You win!";
    			} else {
    				return "You lost to a computer";
    			}
    		}
    		return "Why did you have to test my game like that?!?! Next time choose rock, paper, or scissors!!"
    	};
    	</script>
    </head>
    <body>
    	<button onclick="play('rock')">Rock</button>
    	<button onclick="play('paper')">Paper</button>
    	<button onclick="play('scissors')">Scissors</button>
    	<span id="game"></span>
    </body>
    </html>
    I'm sure you'll copy and paste this code, see that it works and possibly move on, but I'd recommend that you sit down and make sure you understand the difference. It'll be invaluable to you if you know why your code wasn't working and why mine does.

  3. #3
    Join Date
    Mar 2014
    Posts
    6
    Thank you so much! I VERY interested in code. It is a new hobby so I don't know that much. I completed a course on code academy.com on HTML 5. After finishing that and feeling that I understood it pretty well, I decided to take the Java Script one. One of the lessons is making this game. I did it but wanted to do more. One of my classmates is teaching me how to use Jquery. Also I wanted to introduce buttons. Anyway thank you so much for the help. I also have no idea what a CODE bbcode is so I am sorry about that. Oh, I am working in TextWrangler. I don't know if that is good or not or maybe if you had any suggestions(I'm on a mac).
    I just ran the code and oh my goodness it runs amazingly and I love the way it looks. Thanks a ton. Do you know of anyway to implement a automatic scrolling feature or a scoreboard. If you don't know how don't worry you have already helped so much.
    Best regards,
    Kyle Donnelly

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles