By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,407 Members | 2,842 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,407 IT Pros & Developers. It's quick & easy.

prompt not working rock paper scissors odin project

P: 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
Share this Question
Share on Google+
10 Replies


Expert 100+
P: 1,035
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

Expert 100+
P: 1,035
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

P: 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

Expert 100+
P: 1,035
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
Expert Mod 5K+
P: 5,333
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

P: 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

Expert 100+
P: 1,035
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
Expert Mod 5K+
P: 5,333
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

P: 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
Expert Mod 5K+
P: 5,333
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

Post your reply

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