473,395 Members | 1,677 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

prompt not working rock paper scissors odin project

4
Hi, the prompt doesn't work. Any suggestions? If you could spell it out for me it would be helpful. I have only been doing this for less than a week Thanks!

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.   <script>
  5. function askUser(){
  6.  var playerSelection=prompt("Do you choose rock, paper or scissors?")}
  7.  
  8. askUser()
  9.  
  10.  
  11. function computerPlay(){
  12.   let computerSelection = Math.random() *3;
  13.   if (computerSelection===0) {
  14.       computerSelection = "rock";
  15.       } else(computerSelection ===1); {
  16.         computerSelection = "scissors"
  17.       } else {computerSelection = "paper"
  18.              }
  19. }
  20.  
  21.  
  22. function singleRound(playerSelection, computerSelection)
  23.     playerSelection = playerSelection.toLowerCase()
  24.     if (playerSelection=="scissors" && computerSelection=="scissors") {
  25. return "It's a tie!"}
  26.  
  27.     else if 
  28.     (playerSelection=="scissors" && computerSelection=="rock"){
  29. return "Rock bests scissors"}
  30.  
  31.     else if (playerSelection=="scissors" && computerSelection=="paper")
  32.     {return"You win. Scissors beats paper"} 
  33.      else if
  34.   (playerSelection=="paper" && computerSelection=="scissors"){
  35. return "I win. Scissors beats paper!"}
  36.  
  37.     else if
  38. (playerSelection=="paper"/i && computerSelection=="rock") {
  39. return "You win! paper beats rock!"}
  40.  
  41.     else if (playerSelection=="paper" && computerSelection=="paper")
  42.     {return "Paper and paper are a tie!"}
  43.  
  44.     else if (playerSelection=="rock" && computerSelection=="paper"){
  45. return "I win. Paper beats rock."}
  46.  
  47.     else if (playerSelection=="rock" && computerSelection=="scissors"){
  48. return "You win. Rock beats scissors."
  49. }
  50.     else if (playerSelection=="rock" && computerSelection=="rock"){
  51. return "Rock and rock is a tie."
  52.     }
  53.  
  54.  
  55.  
  56.  
  57.   </script>
  58.  
  59. </head>
  60. <body>
  61.  
  62.  
  63. </body>
  64. </html>
Mar 17 '18 #1
10 2309
Luuk
1,047 Expert 1GB
On line #15 you are missing an 'if' (after 'else')
On line #22 you are missing an '{'

After changing above two typo's, you'll get a question asked about "Do you choose rock, paper or scissors?"
Mar 17 '18 #2
Luuk
1,047 Expert 1GB
Also there is something called the scope of a variable, Google for it, an look at what this code does:

Expand|Select|Wrap|Line Numbers
  1. var a = 1;
  2.  
  3. function one() {
  4.           var a=2;
  5.           alert(a);
  6. }
  7.  
  8. one();
  9. alert(a);
  10.  
It gives two alerts, first '2', than '1'.....
It's important to understand why the second alert returns '1'!
Mar 17 '18 #3
as1000
4
Thank you for your answer. I made the changes but it still isn't working.
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.   <script>
  5. function askUser(){
  6.  var playerSelection=prompt("Do you choose rock, paper or scissors?")}
  7.  
  8. askUser()
  9.  
  10.  
  11. function computerPlay(){
  12.   let computerSelection = Math.random() *3;
  13.   if (computerSelection===0) {
  14.       computerSelection = "rock";
  15.       } else if(computerSelection ===1); {
  16.         computerSelection = "scissors"
  17.       } else {computerSelection = "paper"
  18.              }
  19. }
  20.  
  21.  
  22. function singleRound(playerSelection, computerSelection){
  23.     playerSelection = playerSelection.toLowerCase()
  24.     if (playerSelection=="scissors" && computerSelection=="scissors") {
  25. return "It's a tie!"}
  26.  
  27.     else if 
  28.     (playerSelection=="scissors" && computerSelection=="rock"){
  29. return "Rock bests scissors"}
  30.  
  31.     else if (playerSelection=="scissors" && computerSelection=="paper")
  32.     {return"You win. Scissors beats paper"} 
  33.      else if
  34.   (playerSelection=="paper" && computerSelection=="scissors"){
  35. return "I win. Scissors beats paper!"}
  36.  
  37.     else if
  38. (playerSelection=="paper"/i && computerSelection=="rock") {
  39. return "You win! paper beats rock!"}
  40.  
  41.     else if (playerSelection=="paper" && computerSelection=="paper")
  42.     {return "Paper and paper are a tie!"}
  43.  
  44.     else if (playerSelection=="rock" && computerSelection=="paper"){
  45. return "I win. Paper beats rock."}
  46.  
  47.     else if (playerSelection=="rock" && computerSelection=="scissors"){
  48. return "You win. Rock beats scissors."
  49. }
  50.     else if (playerSelection=="rock" && computerSelection=="rock"){
  51. return "Rock and rock is a tie."
  52.     }
  53.  
  54.  
  55.  
  56.  
  57.   </script>
  58.  
  59. </head>
  60. <body>
  61.  
  62.  
  63. </body>
  64. </html>
  65.  
Mar 17 '18 #4
Luuk
1,047 Expert 1GB
Did you read my second post?

If not, or if you did not understand, try adding this line between lines #22 en #23
Expand|Select|Wrap|Line Numbers
  1. alert(playerSelection); alert(computerSelection);
You will see that this code is never executed, and if it is executed that these variabled will not have the value you expect them to have ...
Mar 18 '18 #5
gits
5,390 Expert Mod 4TB
aside the mentioned scope issues - there are even more syntax errors in the code - that could be much better spotted if the code was written somehow with a clean alignment.

line 15: semicolon breaks the conditional statement
line 38: whats with the /i there?

last function body missing a closing curly bracket.
Mar 19 '18 #6
as1000
4
Thanks. I moved the variables outside the functions but here is still no prompt.
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.   <script>
  5.  
  6. var playerSelection=prompt("Do you choose rock, paper or scissors?")}
  7. let computerSelection = Math.random() *3;
  8.  
  9.  
  10.  
  11. function computerPlay(){
  12.  
  13.   if (computerSelection===0) {
  14.       computerSelection = "rock";
  15.       } else if(computerSelection ===1) {
  16.         computerSelection = "scissors"
  17.       } else {computerSelection = "paper"
  18.              }
  19. }
  20.  
  21.  
  22. function singleRound(playerSelection, computerSelection){
  23.     playerSelection = playerSelection.toLowerCase()
  24.     if (playerSelection=="scissors" && computerSelection=="scissors") {
  25. return "It's a tie!"}
  26.  
  27.     else if 
  28.     (playerSelection=="scissors" && computerSelection=="rock"){
  29. return "Rock bests scissors"}
  30.  
  31.     else if (playerSelection=="scissors" && computerSelection=="paper")
  32.     {return"You win. Scissors beats paper"} 
  33.      else if
  34.   (playerSelection=="paper" && computerSelection=="scissors"){
  35. return "I win. Scissors beats paper!"}
  36.  
  37.     else if
  38. (playerSelection=="paper"/i && computerSelection=="rock") {
  39. return "You win! paper beats rock!"}
  40.  
  41.     else if (playerSelection=="paper" && computerSelection=="paper")
  42.     {return "Paper and paper are a tie!"}
  43.  
  44.     else if (playerSelection=="rock" && computerSelection=="paper"){
  45. return "I win. Paper beats rock."}
  46.  
  47.     else if (playerSelection=="rock" && computerSelection=="scissors"){
  48. return "You win. Rock beats scissors."
  49. }
  50.     else if (playerSelection=="rock" && computerSelection=="rock"){
  51. return "Rock and rock is a tie."
  52.     }
  53.     }
  54.  
  55.  
  56.  
  57.  
  58.   </script>
  59.  
  60. </head>
  61. <body>
  62.  
  63.  
  64. </body>
  65. </html>
Mar 19 '18 #7
Luuk
1,047 Expert 1GB
When do you think the code on line #6 (var playerSelection=....) is getting executed?

Because you are not getting the prompt, does mean that this line is not executed.

I suggest to surround line #6 and #7 with:
Expand|Select|Wrap|Line Numbers
  1. function init() {
  2.   var playerSelection=prompt("Do you choose rock, paper or scissors?")}
  3.   let computerSelection = Math.random() *3;
  4. }
and change line #61 to
Expand|Select|Wrap|Line Numbers
  1. body onload="init();"
Mar 19 '18 #8
gits
5,390 Expert Mod 4TB
well - there are still syntax errors in the code:

line 6: closing curly bracket invalidates parsing of the script

line 38: the /i is still there but useless

usually the above hint is correct to start the script in the onload-event of the page since you probably want to output something on the page using the DOM. The prompt actually isnt executed at the moment just because of the syntax error in line 6. But even if it would be executed the other methods arent called at any point - so you need to call them when they should operate.

I suggest to open the browsers developer console when you test your code - that way you can avoid posting all the obvious errors and fix your code faster instead of waiting for replies here. Then you can post back with more detailed questions - if its still not working as you expect it to work.
Mar 20 '18 #9
as1000
4
Hi. Thanks. The prompt worked once. I then called the functions and it stopped working. I then got two errors-1 semicolon after calling a function and one saying that init is not defined.
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.   <script>
  5.  
  6. function init(){
  7.  var playerSelection=prompt("Do you choose rock, paper or scissors?")
  8.  let computerSelection = Math.random() *3;
  9.  }
  10.  
  11.  
  12. computerPlay(){
  13.  
  14.   if (computerSelection===0) {
  15.       computerSelection = "rock";
  16.       } else if(computerSelection ===1) {
  17.         computerSelection = "scissors"
  18.       } else {computerSelection = "paper"
  19.              }
  20. }
  21.  
  22. computerPlay(computerSelection);
  23.  
  24.  
  25. function singleRound(playerSelection, computerSelection){
  26.     playerSelection = playerSelection.toLowerCase()
  27.     if (playerSelection=="scissors" && computerSelection=="scissors") {
  28. return "It's a tie!"}
  29.  
  30.     else if 
  31.     (playerSelection=="scissors" && computerSelection=="rock"){
  32. return "Rock bests scissors"}
  33.  
  34.     else if (playerSelection=="scissors" && computerSelection=="paper")
  35.     {return"You win. Scissors beats paper"} 
  36.      else if
  37.   (playerSelection=="paper" && computerSelection=="scissors"){
  38. return "I win. Scissors beats paper!"}
  39.  
  40.     else if
  41. (playerSelection=="paper" && computerSelection=="rock") {
  42. return "You win! paper beats rock!"}
  43.  
  44.     else if (playerSelection=="paper" && computerSelection=="paper")
  45.     {return "Paper and paper are a tie!"}
  46.  
  47.     else if (playerSelection=="rock" && computerSelection=="paper"){
  48. return "I win. Paper beats rock."}
  49.  
  50.     else if (playerSelection=="rock" && computerSelection=="scissors"){
  51. return "You win. Rock beats scissors."
  52. }
  53.     else if (playerSelection=="rock" && computerSelection=="rock"){
  54. return "Rock and rock is a tie."
  55.     }
  56.     }
  57.  
  58. singleRound(computerSelection, playerSelection);
  59.  
  60.  
  61.  
  62.   </script>
  63.  
  64. </head>
  65. <body onload="init();">
  66.  
  67.  
  68. </body>
  69. </html>
Mar 20 '18 #10
gits
5,390 Expert Mod 4TB
ok - did you have a look at the developer tools? it tells you that it breaks in your line 12 due to a syntax error again. you missing the function keyword there.

and then of course it will complain that init is not defined since the script couldn't be parsed correctly before.

you need to understand what exactly happens when you run a webpage together with javascript. let me summarize it in a few simplified words:

the html doument basically is rendered by the browser - which itself basically builds a in-memory model of that page and provides a (javascript-)API to that in-memory representation of the document. this representation is usually referred to as the DOM (document object model). With the mentioned API its possible to operate on that DOM with javascript to dynamically modify that document - this - in very simplified words - includes appearance and even behavior of the page or even just portions of it. behavior is in most cases triggered by events - like clicking on something, changing a value in an input element or even just loading a page and wait for its DOM to be fully ready created by the browser.

Look at your page now - the browser starts reading it from the top to the bottom - and starts interpreting it to display the html-page. since usually you want to output something in a div or such - you can access the DOM safely when the page is fully interpreted and the DOM is fully created. At that point the onload-event of the document is fired - and you use it to call your init-function.

when you call other functions in the script block directly like you did in your code - those are executed right away when the browser interprets the page - thus everything that you call there is executed before! the onload-event is triggered - or at least it is not safe to rely on something that you expect to have happened before. So you would need to modify your code to have a reliable execution chain of commands that you want to have executed.

I suggest to dig more into what i told you here - since those things are the minimum things to know about when you want to do something right with javascript. Its no rocket-science so you can do it - and you wouldn't drive around a truck on the streets as well without learning some basics before.
Mar 21 '18 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Mathias Soeken | last post by:
Hello NG, Two years ago, I have created a project for web controls (www.pprojekt.de). I am working alone on this project, so I have not done a lot for the project in the last time. But now I...
7
by: TooNaive | last post by:
Hello all, I'm taking a C class and am having to write a program to play a game of rock, paper scissors, and with the output of: You chose paper and I chose rock. You Win where paper is the...
4
by: anonymike | last post by:
Hello, I'm working on an ASP.NET project (VS 2005) utilizing an Oracle database backend. While I understand that Gridview's are a breeze to work with using SQL Server (hundreds of sites point...
2
by: nathan.haywood | last post by:
I saw several posts, but no real answers about this. I've been playing with the Profile classes in a sample Web Site, but cannot seem to get the Profile to work in my actual Web Application...
1
by: flg22 | last post by:
Hi I am working on this Rock, paper, scissors java game and the program works, but I can not figure out how to get the images to load onto the program. So my question is how do I get the images to...
8
by: jmf777 | last post by:
Hi here is my problem I want to have the outcome of my rock paper scissors game to print outcomes like: You chose paper and I chose rock. You win. The value for player_choice and machine_choice is...
2
by: Izkimar | last post by:
I'm supposed to redesign this paper scissors rock game that first happened between 2 players, and for only one round. Yet, now I'm supposed to take for loops and ask them if they want to play...
5
by: ramosariel | last post by:
this is for our project, and i'm still a beginner in programming, so guys I need some help. I want to create a random rock paper scissors game(player vs computer)... I tried many times to...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.