Learn Javascript Rock Paper Scissors Project

const userChoice function (userInput) = {
userInput = userInput.toLowerCase();
if (
userInput === “rock” ||
userInput === “scissors” ||
userInput === “paper” ||
userInput === ‘bomb’
) {
return userInput;
} else {
console.log(“Error, please type: rock, paper or scissors.”);
}

if (userChoice == ‘bomb’) {
return ‘Congratulations, you won!’
}
}

const computerChoice function() = {
const randomNumber = Math.floor(Math.random() * 3);

switch (randomNumber) {
case 0:
return “rock”;
case 1:
return “paper”;
case 2:
return “scissors”;}

const determineWinner function(userChoice, computerChoice)
{
if (userChoice === computerChoice) {
return “This game is a tie!”;
}

if (userChoice === “rock”) {
if (computerChoice === “paper”) {
return “sorry, computer won!”;
} else {
return “Congratulations, you won!”;
}
}

if (userChoice === “paper”) {
if (computerChoice === “scissors”) {
return “Sorry, computer won!”;
} else {
return “Congratulations, you won!”;
}
}

if (userChoice === “scissors”) {
if (computerChoice === “paper”) {
return “Sorry, computer won!”;
} else {
return “Congratulations, you won!”;
}
if(userChoice === ‘bomb’) {
return ‘Congratulations, you won!’;
}
}

const playgame function() {
const userChoice = getUserChoice(‘paper’);
const computerChoice = getComputerChoice();
console.log('You threw: ’ + userChoice);
console.log('The computer threw: ’ + computerChoice);

console.log(determineWinner(userChoice, computerChoice));
}

playGame()```

const userChoice = function (userInput) {

The function expression is assigned to the variable reference.


To show how trivial the naming is,

let f = userChoice

f now refers to the same function expression. They point to the same object.

const userChoice = function (userInput) = {
  userInput = userInput.toLowerCase();
  if (
    userInput === "rock" ||
    userInput === "scissors" ||
    userInput === "paper" ||
    userInput === 'bomb'
  ) {
    return userInput;
  } else {
    console.log("Error, please type: rock, paper or scissors.");
  }

  if (userChoice == 'bomb') {
    return 'Congratulations, you won!'
  }
}

const computerChoice function() = {
  const randomNumber = Math.floor(Math.random() * 3);

  switch (randomNumber) {
    case 0:
      return "rock";
    case 1:
      return "paper";
    case 2:
      return "scissors";}


const determineWinner function(userChoice, computerChoice)
{
  if (userChoice === computerChoice) {
    return "This game is a tie!";
}

if (userChoice === "rock") {
  if (computerChoice === "paper") {
    return "sorry, computer won!";
  } else {
    return "Congratulations, you won!";
  }
}

if (userChoice === "paper") {
  if (computerChoice === "scissors") {
    return "Sorry, computer won!";
  } else {
    return "Congratulations, you won!";
  }
}

if (userChoice === "scissors") {
  if (computerChoice === "paper") {
    return "Sorry, computer won!";
  } else {
    return "Congratulations, you won!";
  }
  if(userChoice === 'bomb') {
    return 'Congratulations, you won!';
  }
}

const playgame function() {
  const userChoice = getUserChoice('paper');
  const computerChoice = getComputerChoice();
  console.log('You threw: ' + userChoice);
  console.log('The computer threw: ' + computerChoice);

  console.log(determineWinner(userChoice, computerChoice)); 
} 

playGame()

Error Message:
/home/ccuser/workspace/javascript_101_Unit_3_v2/rockPaperScissors.js:1
(function (exports, require, module, __filename, __dirname) { const userChoice = function (userInput) = {
^
SyntaxError: Unexpected token =
at createScript (vm.js:53:10)
at Object.runInThisContext (vm.js:95:10)
at Module._compile (module.js:543:28)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)

There should not be an = in front of the opening brace, only after the variable name (before the ‘function’ keyword).

const userChoice = function (userInput) {
  userInput = userInput.toLowerCase();
  if (
    userInput === "rock" ||
    userInput === "scissors" ||
    userInput === "paper" ||
    userInput === 'bomb'
  ) {
    return userInput;
  } else {
    console.log("Error, please type: rock, paper or scissors.");
  }

  if (userChoice == 'bomb') {
    return 'Congratulations, you won!'
  }
}

const computerChoice function () = {
  const randomNumber = Math.floor(Math.random() * 3);

  switch (randomNumber) {
    case 0:
      return "rock";
    case 1:
      return "paper";
    case 2:
      return "scissors";}


const determineWinner function (userChoice, computerChoice)
{
  if (userChoice === computerChoice) {
    return "This game is a tie!";
}

if (userChoice === "rock") {
  if (computerChoice === "paper") {
    return "sorry, computer won!";
  } else {
    return "Congratulations, you won!";
  }
}

if (userChoice === "paper") {
  if (computerChoice === "scissors") {
    return "Sorry, computer won!";
  } else {
    return "Congratulations, you won!";
  }
}

if (userChoice === "scissors") {
  if (computerChoice === "paper") {
    return "Sorry, computer won!";
  } else {
    return "Congratulations, you won!";
  }
  if(userChoice === 'bomb') {
    return 'Congratulations, you won!';
  }
}

const playgame function() {
  const userChoice = getUserChoice('paper');
  const computerChoice = getComputerChoice();
  console.log('You threw: ' + userChoice);
  console.log('The computer threw: ' + computerChoice);

  console.log(determineWinner(userChoice, computerChoice)); 
} 

playGame()

Okay I have placed the equals sign right after the variable name.

Remember, after the variable, not before the opening brace.

Insert = between variable and function keyword.

Same again, = after the variable.

const playGame = function () {

}
1 Like

const userChoice = function (userInput) {
userInput = userInput.toLowerCase();
if (
userInput === “rock” ||
userInput === “scissors” ||
userInput === “paper” ||
userInput === ‘bomb’
) {
return userInput;
} else {
console.log(“Error, please type: rock, paper or scissors.”);
}

if (userChoice == ‘bomb’) {
return ‘Congratulations, you won!’
}
}

const computerChoice = function () {
const randomNumber = Math.floor(Math.random() * 3);

switch (randomNumber) {
case 0:
return “rock”;
case 1:
return “paper”;
case 2:
return “scissors”;}

const determineWinner = function (userChoice, computerChoice)
{
if (userChoice === computerChoice) {
return “This game is a tie!”;
}

if (userChoice === “rock”) {
if (computerChoice === “paper”) {
return “sorry, computer won!”;
} else {
return “Congratulations, you won!”;
}
}

if (userChoice === “paper”) {
if (computerChoice === “scissors”) {
return “Sorry, computer won!”;
} else {
return “Congratulations, you won!”;
}
}

if (userChoice === “scissors”) {
if (computerChoice === “paper”) {
return “Sorry, computer won!”;
} else {
return “Congratulations, you won!”;
}
if(userChoice === ‘bomb’) {
return ‘Congratulations, you won!’;
}
}

const playgame = function() {
const userChoice = getUserChoice(‘paper’);
const computerChoice = getComputerChoice();
console.log('You threw: ’ + userChoice);
console.log('The computer threw: ’ + computerChoice);

console.log(determineWinner(userChoice, computerChoice));
}

playGame()

I had thought that I had edited that. Sorry about that.

I believe I have made the changes requested.

Error message now presented:

/home/ccuser/workspace/javascript_101_Unit_3_v2/rockPaperScissors.js:88
});
^
SyntaxError: Unexpected token )
at createScript (vm.js:53:10)

1 Like

Are you getting any more errors?

Do one more complete audit of the braces.

Error message:

/home/ccuser/workspace/javascript_101_Unit_3_v2/rockPaperScissors.js:88
});
^
SyntaxError: Unexpected token )
at createScript (vm.js:53:10)
at Object.runInThisContext (vm.js:95:10)
at Module._compile (module.js:543:28)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)

I think it is acting like there is no end to the code because the last line if you include the comments is line 83

unexpected ) …

looking

I am calling the playGame() function on line 73

playGame

JS looks for exactly what we give it.

const userChoice = function (userInput) {
  userInput = userInput.toLowerCase();
  if (
    userInput === "rock" ||
    userInput === "scissors" ||
    userInput === "paper" ||
    userInput === 'bomb'
  ) {
    return userInput;
  } else {
    console.log("Error, please type: rock, paper or scissors.");
  }

  if (userChoice == 'bomb') {
    return 'Congratulations, you won!'
  }
}

const computerChoice = function () {
  const randomNumber = Math.floor(Math.random() * 3);

  switch (randomNumber) {
    case 0:
      return "rock";
    case 1:
      return "paper";
    case 2:
      return "scissors";}


const determineWinner = function (userChoice, computerChoice)
{
  if (userChoice === computerChoice) {
    return "This game is a tie!";
}

if (userChoice === "rock") {
  if (computerChoice === "paper") {
    return "sorry, computer won!";
  } else {
    return "Congratulations, you won!";
  }
}

if (userChoice === "paper") {
  if (computerChoice === "scissors") {
    return "Sorry, computer won!";
  } else {
    return "Congratulations, you won!";
  }
}

if (userChoice === "scissors") {
  if (computerChoice === "paper") {
    return "Sorry, computer won!";
  } else {
    return "Congratulations, you won!";
  }
  if(userChoice === 'bomb') {
    return 'Congratulations, you won!';
  }
}

const playgame  = function() {
  const userChoice = getUserChoice('paper');
  const computerChoice = getComputerChoice();
  console.log('You threw: ' + userChoice);
  console.log('The computer threw: ' + computerChoice);

  console.log(determineWinner(userChoice, computerChoice)); 
} 

playgame()

Error:
/home/ccuser/workspace/javascript_101_Unit_3_v2/rockPaperScissors.js:84
});
^
SyntaxError: Unexpected token )

I checked the braces and the parentheses. They seem to have matching sets! Its so strange!

Closing brace of function body is missing.


Aside

In a switch statement it is often preferred to not indent the case, but rather leave it in line with the switch keyword.

switch (expression) {
case 0:
    return ...
case 1:
    return ...
default:
    return ...
}

Question: Wouldn’t it need to be at the bottom to include all of the code inside of the braces?

Oh wait a minute. The closing brace on the switch is complete only because I had left a closing brace for the computerChoice function.

I really appreciate what your doing!

1 Like
const userChoice = function (userInput) {
  userInput = userInput.toLowerCase();
  if (
    userInput === "rock" ||
    userInput === "scissors" ||
    userInput === "paper" ||
    userInput === 'bomb'
  ) {
    return userInput;
  } else {
    console.log("Error, please type: rock, paper or scissors.");
  }

  if (userChoice == 'bomb') {
    return 'Congratulations, you won!'
  }
}

const computerChoice = function () {
  const randomNumber = Math.floor(Math.random() * 3);

  switch (randomNumber) {
    case 0:
      return "rock";
    case 1:
      return "paper";
    case 2:
      return "scissors";}
}


const determineWinner = function (userChoice, computerChoice)
{
  if (userChoice === computerChoice) {
    return "This game is a tie!";
}

if (userChoice === "rock") {
  if (computerChoice === "paper") {
    return "sorry, computer won!";
  } else {
    return "Congratulations, you won!";
  }
}

if (userChoice === "paper") {
  if (computerChoice === "scissors") {
    return "Sorry, computer won!";
  } else {
    return "Congratulations, you won!";
  }
}

if (userChoice === "scissors") {
  if (computerChoice === "paper") {
    return "Sorry, computer won!";
  } else {
    return "Congratulations, you won!";
  }
  if(userChoice === 'bomb') {
    return 'Congratulations, you won!';
  }
}

const playgame  = function() {
  const userChoice = getUserChoice('paper');
  const computerChoice = getComputerChoice();
  console.log('You threw: ' + userChoice);
  console.log('The computer threw: ' + computerChoice);

  console.log(determineWinner(userChoice, computerChoice)); 
} 

playgame()

Error Message:

/home/ccuser/workspace/javascript_101_Unit_3_v2/rockPaperScissors.js:85
});
^
SyntaxError: Unexpected token )
at createScript (vm.js:53:10)

found one error in determineWinner with a missing closing brace. one sec.

1 Like
const userChoice = function (userInput) {
  userInput = userInput.toLowerCase();
  if (
    userInput === "rock" ||
    userInput === "scissors" ||
    userInput === "paper" ||
    userInput === 'bomb'
  ) {
    return userInput;
  } else {
    console.log("Error, please type: rock, paper or scissors.");
  }

  if (userChoice == 'bomb') {
    return 'Congratulations, you won!'
  }
}

const computerChoice = function () {
  const randomNumber = Math.floor(Math.random() * 3);

  switch (randomNumber) {
    case 0:
      return "rock";
    case 1:
      return "paper";
    case 2:
      return "scissors";}
}


const determineWinner = function (userChoice, computerChoice)
{
  if (userChoice === computerChoice) {
    return "This game is a tie!";
}
}
if (userChoice === "rock") {
  if (computerChoice === "paper") {
    return "sorry, computer won!";
  } else {
    return "Congratulations, you won!";
  }
}

if (userChoice === "paper") {
  if (computerChoice === "scissors") {
    return "Sorry, computer won!";
  } else {
    return "Congratulations, you won!";
  }
}

if (userChoice === "scissors") {
  if (computerChoice === "paper") {
    return "Sorry, computer won!";
  } else {
    return "Congratulations, you won!";
  }
  if(userChoice === 'bomb') {
    return 'Congratulations, you won!';
  }
}

const playgame  = function() {
  const userChoice = getUserChoice('paper');
  const computerChoice = getComputerChoice();
  console.log('You threw: ' + userChoice);
  console.log('The computer threw: ' + computerChoice);

  console.log(determineWinner(userChoice, computerChoice)); 
} 

playgame()

error:
/home/ccuser/workspace/javascript_101_Unit_3_v2/rockPaperScissors.js:66
const userChoice = getUserChoice(‘paper’);
^

ReferenceError: getUserChoice is not defined
at playgame (/home/ccuser/workspace/javascript_101_Unit_3_v2/rockPaperScissors.js:66:22)

I am thinking I convert getUserChoice into userChoice

When writing code from scratch, I like to template the syntax bits, first.

const ____ = function (____) {
  // code in here
}

Same applies to the component parts. Build out the syntax then fill in the bits.

1 Like