LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-01-2013, 01:31 PM   #1
fakie_flip
Senior Member
 
Registered: Feb 2005
Location: San Antonio, Texas
Distribution: Gentoo Hardened using OpenRC not Systemd
Posts: 1,495

Rep: Reputation: 85
Java script does not work from a web browser


Code:
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(choice1, choice2) {   if (choice1 === choice2)   {     return("The result is a tie!");   }   else   {     if (choice1 === "rock") {       if (choice2 === "scissors")       {         return("rock wins");       }       else {         return("paper wins");       }     }   }   if (choice1 === "paper")   {     if (choice2 === "rock")     {       return("paper wins");     }     else {       return("scissors wins");     }   }   if (choice1 === "scissors")   {     if (choice2 === "paper")     {       return("scissors wins");     }     else {       return("rock wins");     }   } };

This code works when running through a javascript engine, but it's not working from a webserver. What must I do, so that it will work correctly in a web browser if i added the code to my vps?

http://www.codecademy.com/courses/ja...s/3/scratchpad
 
Old 04-02-2013, 05:01 AM   #2
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu, mint, suse
Posts: 1,591

Rep: Reputation: 145Reputation: 145
1. You prompt a message but the reply is not connected to the function you wrote.

2. The function returns a string but this will not be displayed even if you connect the function to the OK-button. You could use console.log(string) or alert(string) instead of return.

Last edited by j-ray; 04-02-2013 at 05:03 AM.
 
Old 04-02-2013, 05:04 PM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
Remember that JavaScript is included in a web-page either in "script" tags or by including a reference to an external library in the HTML page. (Take a look at the source code of any web-page to see what I mean.)

Then, something in the web page must cause that script to be executed in response to some event ... such as the clicking of a mouse button.

There are plentiful examples and tutorials on the Internet already.

Since this is obviously intended as an educational exercise, seek the answer yourself. This is very critical to the learning process. A very important skill that you absolutely must master, is to discover how to frame a problem and to find the answers that you are challenged to produce.

Last edited by sundialsvcs; 04-02-2013 at 05:07 PM.
 
Old 04-03-2013, 08:27 AM   #4
fakie_flip
Senior Member
 
Registered: Feb 2005
Location: San Antonio, Texas
Distribution: Gentoo Hardened using OpenRC not Systemd
Posts: 1,495

Original Poster
Rep: Reputation: 85
Quote:
Originally Posted by j-ray View Post
1. You prompt a message but the reply is not connected to the function you wrote.

2. The function returns a string but this will not be displayed even if you connect the function to the OK-button. You could use console.log(string) or alert(string) instead of return.
http://vpaste.net/YMkDb

I changed the code to use alert() like you said but still I am brought to a blank page after submitting an answer.

It is hosted here.

http://expectusafterlun.ch/rps.html

EDIT:

After looking at some examples, I removed the ; after the alert() but still, it is not working =/

Last edited by fakie_flip; 04-03-2013 at 08:31 AM.
 
Old 04-03-2013, 08:58 AM   #5
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu, mint, suse
Posts: 1,591

Rep: Reputation: 145Reputation: 145
You have to call the function you created. I have given the function a name now.

Code:
<script type="text/javascript">


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"; 
} 
compare(userChoice, computerChoice );
function compare (choice1, choice2) {  
	
	if (choice1 === choice2)   {     
		return("The result is a tie!");   
	}   else   {     
		if (choice1 === "rock") {       
			if (choice2 === "scissors")       {         
				alert("rock wins");       
			}       
			else {        
				alert("paper wins");       
			}     
		}   
	}   
	if (choice1 === "paper")   {     
		if (choice2 === "rock")     {       
			alert("paper wins");     
		}     else {       
			alert("scissors wins");     
		}  
	}   
	if (choice1 === "scissors")   {     
		if (choice2 === "paper")     {       
			alert("scissors wins");     
		}     
		else {       
			alert("rock wins");     
		}   
	}
};

</script>

Last edited by j-ray; 04-03-2013 at 09:05 AM. Reason: made a mistake
 
Old 04-03-2013, 09:14 AM   #6
fakie_flip
Senior Member
 
Registered: Feb 2005
Location: San Antonio, Texas
Distribution: Gentoo Hardened using OpenRC not Systemd
Posts: 1,495

Original Poster
Rep: Reputation: 85
Works now

http://expectusafterlun.ch/rps.html

Last edited by fakie_flip; 04-03-2013 at 11:58 AM.
 
Old 04-03-2013, 11:58 AM   #7
fakie_flip
Senior Member
 
Registered: Feb 2005
Location: San Antonio, Texas
Distribution: Gentoo Hardened using OpenRC not Systemd
Posts: 1,495

Original Poster
Rep: Reputation: 85
Thanks for the reply j-ray. I had done it a similar way:

http://vpaste.net/TMbsR
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Java script does not work from a web browser fakie_flip Programming 1 04-01-2013 04:25 PM
Java script does not work from a web browser fakie_flip Programming 1 04-01-2013 12:37 PM
Need help with Web Browser - Java and more KirkShanahan Debian 3 12-30-2008 11:23 AM
Java and Web Browser problems. RHLinuxGUY Linux - Software 1 09-25-2005 11:01 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:46 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration