-3

I am creating a game of Rock, Paper, scissors on "Codecademy" so I am using simple JavaScript for this. There are four necessities for this simple code

  • If computerChoice is between 0 and 0.33, make computerChoice equal to "rock".
  • If computerChoice is between 0.34 and 0.66, make computerChoice equal to "paper".
  • If computerChoice is between 0.67 and 1, make computerChoice equal to "scissors".
  • Needs to be an If/Else If/Else statement

So basically, I need to change the value of the function "computerChoice" based on the value of the function "computerChoice".

    var userChoice = prompt("Do you choose rock, paper or scissors?");

var computerChoice = Math.random(); {
    console.log(computerChoice);
}

if (computerChoice >= 0 & <= 0.33) {
    computerChoice = rock;
}

else if (computerChoice >= 0.34 & < 0.66) {
    computerChoice = paper;
}
else {
    computerChoice = scissors;
}
  • 2
    what problem are you getting? – Mritunjay Jan 15 '15 at 03:49
  • You need several lessons in very basic Javascript principles. I'd suggest to you that it would be more efficient for you to do some studying of basic Javascript syntax rather than ask this level of question here every time you try something for the first time. – jfriend00 Jan 15 '15 at 03:54
  • Your question, which looks like one of more general learning would be better asked at http://codereview.stackexchange.com/ which is more geared to code review style questions. This site is for solving specific issues that you may have. – Steve Mitcham Jan 15 '15 at 03:58

4 Answers4

1

There are following problems in your code.

1) There should be && instead of &.

2) This condition

(computerChoice >= 0 & <= 0.33)

should be

(computerChoice >= 0 && computerChoice <= 0.33)

3) The rock, scissors and paper should be string, just wrap them in quotes.

4) In lines bellow

var computerChoice = Math.random(); {
    console.log(computerChoice);
}

there is not need of { } it should be written as

var computerChoice = Math.random(); 
console.log(computerChoice);

Combining all in one

var userChoice = prompt("Do you choose rock, paper or scissors?");

var computerChoice = Math.random();
console.log(computerChoice);


if (computerChoice >= 0 && computerChoice <= 0.33) {
    computerChoice = 'rock';
} else if (computerChoice >= 0.34 && computerChoice < 0.66) {
    computerChoice = 'paper';
} else {
    computerChoice = 'scissors';
}
Mritunjay
  • 25,338
  • 7
  • 55
  • 68
1
if (computerChoice < 0.33) {
  computerChoice = "rock";  
} else if (computerChoice <= 0.66){
  computerChoice = "paper";    
} else {
  computerChoice = "scissors";
} 
0

Five problems

  1. You don't need braces around the console.log statement.
  2. You need quotes around your values, rock, paper, and scissors (or else define them as variables beforehand).
  3. A logical and in JavaScript requires two ampersands &&. (A single ampersand is a bitwise operator.)
  4. You have to include both sides of the inequality after the logical and. You cannot say & < 0.66 because JavaScript has no idea what you are referring to, but would need to write && computerChoice < 0.66.
  5. A mathematical problem is that you're not quite partitioning the interval from 0 to 1 correctly into three equal segments (and completely leaving out the part between .33 and .34).

Plus one optimization

  • Although 4. is true above, you don't even need the logical && operation at all, because you already know by the time that the code in the else if statement is reached, that the first if conditional is not true, and it does not need to be checked again.

There is no variable you defined called rock, so you can define one, or just use the string "rock". And the same for "paper" and "scissors".

Suggested revision

var userChoice = prompt("Do you choose rock, paper or scissors?");

var computerChoice = Math.random();
console.log(computerChoice);


if (computerChoice < 1/3) {
    computerChoice = "rock";
}

else if (computerChoice < 2/3) {
    computerChoice = "paper";
}

else {
    computerChoice = "scissors";
}
Community
  • 1
  • 1
Joseph Myers
  • 6,434
  • 27
  • 36
0

Your syntax needs a bit of fixing.

For one thing, you don't need those curly braces around the log() statement.

For another thing, you need a variable to test against in the second part of each if statement:

if (computerChoice >= 0 && computerChoice <= 0.33) {
    computerChoice = rock;
} else if (computerChoice >= 0.34 && computerChoice < 0.66) {
    computerChoice = paper;
} else {
    computerChoice = scissors;
}

Note the computerChoice being compared in the second half of each boolean expression.

Also, you haven't defined the variables rock, paper, and scissors. Instead, you can try using the strings 'rock', 'paper', and 'scissors', respectively.

APerson
  • 8,140
  • 8
  • 35
  • 49