JavaScript help

NickHouston

WaLLy's Personal Favorite Krew Member
|K3| Member
|K3| Media Team
Hey guys,
Last night I started playing with JavaScript for the first time. I wrote my code in notepad, checked it with a javascript checker tool and have completed my first text-based game.

How do I actually make it into a game?
 

WaLLy

Lieutenant General
|K3| Executive
Please wait for our Krew Coder Kreubs to come online. I've put you in the queue for assistance. Current wait time: 5-7 hours.

edit:
I'm of no help, but I wanna see this game. Any screenshots?
 

NickHouston

WaLLy's Personal Favorite Krew Member
|K3| Member
|K3| Media Team
It's only a simple text based rock paper scissors game lol.

I need to figure out how to put the code into a file or something so I can play it
 

Sodalover

Second Lieutenant
|K3| Member
On a relaed topic, if you are interested, there's a program called greenfoot in java that allows you to create games and many different other applications. Java isn't related to JavaScript in any way, so it might be confusing at first.
 

Kreubs

|K3|Minecraft Admin
|K3| Executive
Code plz :D

Is it in an html file? JavaScript can't run outside of a web browser.
 

NickHouston

WaLLy's Personal Favorite Krew Member
|K3| Member
|K3| Media Team
it can't?:(

How can I make this playable then? I don't have a website.

And sure here ya go. I know it's messy lol.

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
var compare = function (userChoice, computerChoice) {
if (userChoice === computerChoice) {
return ("The result is a tie!");
}
if (userChoice === "rock") {
if (computerChoice === "scissors") {
return ("Rock Wins");
} else {
return ("Paper Wins");
}
if (userChoice === "paper") {
if (computerChoice === "scissors") {
return ("Scissors Wins!");
} else {
return ("Paper Wins!");
}
if (userChoice === "scissors") {
if (computerChoice === "rock") {
return ("Rock Wins");
} else {
return ("Scissors Wins");
}}}}};
console.log(compare(userChoice, computerChoice))

- - - Updated - - -

the smiley screwed up the top D:
 

MikeK

Vodka supplier
|K3| Member
1) You can click edit -> disable smileys in text.
2) Sorry for not replying on steam yesterday , I was very busy and I know nothing about JavaScript and most other PC issues :K
 
Last edited:

Kreubs

|K3|Minecraft Admin
|K3| Executive
it can't?:(

How can I make this playable then? I don't have a website.

You don't need a website to run html code. When you load a website, the server just sends your browsers some files (the structural files are html) which are then interpreted as the website. You can load webpages simply by double clicking on html files or by dragging and dropping them into a browser.

I tweaked it a little bit as to not allow improper choices, and the results pop up in a text box instead of the console. The results are a bit confusing though, if you somehow forget what your choice was :K

Just save this code with any name using .html as the extension and run it in a browser. Refresh to play again.

Code:
<html>
	<head>
	</head>
	
	<body>
		<script>
			var userChoice = prompt("Do you choose rock, paper or scissors?");
			
			var computerChoice = Math.random();
			
			if (computerChoice < 0.34) {
				computerChoice = "rock";
			} else if(computerChoice <= 0.67) {
				computerChoice = "paper";
			} else {
				computerChoice = "scissors";
			}
			
			var compare = function (userChoice, computerChoice) {
				if(userChoice != "rock" && userChoice != "paper" && userChoice != "scissors") {
					return ("You need to pick rock, paper, or scissors!");
				}
			
				if (userChoice === computerChoice) {
					return ("The result is a tie!");
				}
				if (userChoice === "rock") {
					if (computerChoice === "scissors") {
						return ("Rock Wins"); //computer wins
					} else {
						return ("Paper Wins");
					}
				}
					
				if (userChoice === "paper") {
					if (computerChoice === "scissors") {
						return ("Scissors Wins!"); //computer wins
					} else {
						return ("Paper Wins!");
					}
				}
						
				if (userChoice === "scissors") {
					if (computerChoice === "rock") {
						return ("Rock Wins"); //computer wins
					} else {
						return ("Scissors Wins");
					}
				}
			};
			
			var result = compare(userChoice, computerChoice);
			alert(result);
		</script>
	</body>
</html>
 

NickHouston

WaLLy's Personal Favorite Krew Member
|K3| Member
|K3| Media Team
awesome. Thanks Kreubs:p

I'll make better stuff once I figure out what I'm doing.
 

Kreubs

|K3|Minecraft Admin
|K3| Executive
np :D

Python might be a more simple route, IMO. It can be quite a bit easier to understand than JavaScript.
 
Top Bottom