I need help with my code

hi guys i need a help

const getUserChoice = (userInput) => { userInput = userInput.toLowerCase(); if ( userInput === "rock" || userInput === "paper" || userInput === "scissors" || userInput === "bomb" ) { return userInput; } else { return "Error!"; } }; const getComputerChoice = () => { const randomNumber = Math.floor(Math.random() * 3); switch (randomNumber) { case 0: return "rock"; break; case 1: return "paper"; break; case 2: return "scissors"; } }; const determineWinner = (userChoice, computerChoice) => { if (userChoice === computerChoice) { return "The game is a tie"; } if (userChoice === "rock") { if (computerChoice === "paper") { return "The computer won!"; } else { return "user won"; } } if (userChoice === "paper") { if (computerChoice === "scissors") { return "computer won"; } else { return "user won"; } } if (userChoice === "scissors") { if (computerChoice === "rock") { return "computer won"; } else { return "user won"; } } if (userChoice === "bomb") { return "detavitca , you won"; } }; const playGame = () => { const userChoice = getUserChoice("bomb"); const computerChoice = getComputerChoice(); console.log( `your choice was ${userChoice} and the computer choose ${computerChoice} so = ` ); console.log(determineWinner(userChoice, computerChoice)); }; playGame();

here is my code at the end of the rock,paper,scissor project in the functions syllabus
it works fine except when the const userChoice = getUserChoice("bomb"); in the line 62 is empty like thisconst userChoice = getUserChoice(" "); the output is your choice was Error! and the computer choose paper so = undefined i tried to enhace/change the code to =

const playGame = () => {
  const userChoice = getUserChoice("");
  const computerChoice = getComputerChoice();
  if (userChoice === "") {
    return "";
  } else {
    console.log(
      `your choice was ${userChoice} and the computer choose ${computerChoice} so = `
    );
  }

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

but i still get the same output your choice was Error! and the computer choose rock so = undefined (i just want so nothing get log on the terminal)
i tried to do it with logical operations and it ended up like this

const playGame = () => {
  const userChoice = getUserChoice("");
  const computerChoice = getComputerChoice();
  if (userChoice !== "") {
    return "";
  } else {
    console.log(
      `your choice was ${userChoice} and the computer choose ${computerChoice} so = `
    );
  }

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

but now nothing will be shown at the terminal no matter whta i type in const userChoice = getUserChoice("//here no matter what i type");
can you please help me i even tried couple of other ways (i just dont type them here so it wont get longer than it is )
can someone please help me
@mtf (i tag you specifically , because i know you can help me faster)
thanks

1 Like

It can never be "" since your function is returning, 'Error!'

2 Likes

so if i delete Error! is it ging to work ?
let me check

1 Like

i deleted the return "Error!" it didnt work either

1 Like

You don’t need to remove the return statement, just test for what it returns:

if (userChoice !== 'Error!') {

}
2 Likes

ok here is the updated code

const getUserChoice = (userInput) => { userInput = userInput.toLowerCase(); if ( userInput === "rock" || userInput === "paper" || userInput === "scissors" || userInput === "bomb" ) { return userInput; } else { return "Error!"; } }; const getComputerChoice = () => { const randomNumber = Math.floor(Math.random() * 3); switch (randomNumber) { case 0: return "rock"; break; case 1: return "paper"; break; case 2: return "scissors"; } }; const determineWinner = (userChoice, computerChoice) => { if (userChoice === computerChoice) { return "The game is a tie"; } if (userChoice === "rock") { if (computerChoice === "paper") { return "The computer won!"; } else { return "user won"; } } if (userChoice === "paper") { if (computerChoice === "scissors") { return "computer won"; } else { return "user won"; } } if (userChoice === "scissors") { if (computerChoice === "rock") { return "computer won"; } else { return "user won"; } } if (userChoice === "bomb") { return "detavitca , you won"; } }; const playGame = () => { const userChoice = getUserChoice(""); const computerChoice = getComputerChoice(); if (userChoice === "Error!") { return ""; } else { console.log( `your choice was ${userChoice} and the computer choose ${computerChoice} so = ` ); } console.log(determineWinner(userChoice, computerChoice)); }; playGame();

i did it at line 64 and yes it work if i dont type and of the aviable option if will show nothing just like i wanted but there is another problem at line 65 if i write anything

if (userChoice === "Error!") {
    return "//anything here";

it wont log to the console

1 Like

Line 67 should be a return statement, not a log out. You are logging the returns on line 72.

1 Like

i see ,
so i think i did it fase all the time
look the thing i want is that =
1 = when there is no userChoice in the console we see a message hat says please choose
2 = if the input was wrong (something except of rock,paper,scissors) the cosole say to choose the correct one

1 Like

we alread did the number 2 , becuase when the input is wrong there will be nothing in the console (or if i remove the line 64 it will say Error! but we have another problem if i dod that the output will be like this = your choice was Error! and the computer choose paper so = undefined
what should i do about it

1 Like

If you haven’t solved this by tomorrow, tag me in again. Spend some time going through the logic, given what you now know and see if you can solve this on your own.

3 Likes

ok i will , until tomorrow

1 Like

bro i have kindof solve it
ive solve these =

1 = when there is no userChoice (at line 62) in the console we see a message
 that says : choose something
2 = if the input was wrong (something except of rock,paper,scissors) the cosole 
 say:  choose the correct one

but also this = your choice was undefined and the computer choose scissors so = undefined will get logged to the console aswell

the problem is (if the problem is that , because i dont know what ese is the problem) line 64 until line 67 wont get execute
i’ve tried to replace userChoice at line 64 and 66 with userInput but then i get this error = ReferenceError: userInput is not defined i tried to replace them also with getUserChoice didnt work either ,
i know i must replace 64 and 66 with something but with what ? (i tried the things i could think of)

here is the code =

const getUserChoice = (userInput) => { userInput = userInput.toLowerCase(); if ( userInput === "rock" || userInput === "paper" || userInput === "scissors" || userInput === "bomb" ) { return userInput; } else if (userInput === "") { console.log("choose something"); } else { console.log("choose the correct one"); } }; const getComputerChoice = () => { const randomNumber = Math.floor(Math.random() * 3); switch (randomNumber) { case 0: return "rock"; break; case 1: return "paper"; break; case 2: return "scissors"; } }; const determineWinner = (userChoice, computerChoice) => { if (userChoice === computerChoice) { return "The game is a tie"; } if (userChoice === "rock") { if (computerChoice === "paper") { return "computer won"; } else { return "user won"; } } if (userChoice === "paper") { if (computerChoice === "scissors") { return "computer won"; } else { return "user won"; } } if (userChoice === "scissors") { if (computerChoice === "rock") { return "computer won"; } else { return "user won"; } } if (userChoice === "bomb") { return "detavitca , you won"; } }; const playGame = () => { const userChoice = getUserChoice(""); const computerChoice = getComputerChoice(); if (userChoice === "choose something") { console.log; } else if (userChoice === "choose the correct one") { console.log; } else { } console.log( `your choice was ${userChoice} and the computer choose ${computerChoice} so = ` ); console.log(determineWinner(userChoice, computerChoice)); }; playGame();
1 Like

@mtf , i failed and i accept my failure, can you help me :pray:

1 Like

At this point my help is not what you need. You need to go back to the beginning and get your house in order. This is not a field where you can get others to do your learning for you. You have to do that yourself.

Keep trying, but give yourself a blueprint (a sketch on paper) and make your logic work. By tomorrow you may have succeeded. If not, post your entire code in formatted form (raw text in a post) not in that useless codebyte.

Posting formatted code is another thing you might have to learn. Look for the instructions in the New User section.

2 Likes

by formatting code you mean =

[codebyte language=javascript]
const getUserChoice = (userInput) => {
  userInput = userInput.toLowerCase();
  if (
    userInput === "rock" ||
    userInput === "paper" ||
    userInput === "scissors" ||
    userInput === "bomb"
  ) {
    return userInput;
  } else if (userInput === "") {
    console.log("choose something");
  } else {
    console.log("choose the correct one");
  }
};

const getComputerChoice = () => {
  const randomNumber = Math.floor(Math.random() * 3);
  switch (randomNumber) {
    case 0:
      return "rock";
      break;
    case 1:
      return "paper";
      break;
    case 2:
      return "scissors";
  }
};

const determineWinner = (userChoice, computerChoice) => {
  if (userChoice === computerChoice) {
    return "The game is a tie";
  }
  if (userChoice === "rock") {
    if (computerChoice === "paper") {
      return "computer won";
    } else {
      return "user won";
    }
  }
  if (userChoice === "paper") {
    if (computerChoice === "scissors") {
      return "computer won";
    } else {
      return "user won";
    }
  }
  if (userChoice === "scissors") {
    if (computerChoice === "rock") {
      return "computer won";
    } else {
      return "user won";
    }
  }
  if (userChoice === "bomb") {
    return "detavitca , you won";
  }
};

const playGame = () => {
  const userChoice = getUserChoice("");
  const computerChoice = getComputerChoice();
  if (userChoice === "choose something") {
    console.log;
  } else if (userChoice === "choose the correct one") {
    console.log;
  } else {
  }
  console.log(
    `your choice was ${userChoice} and the computer choose ${computerChoice} so = `
  );

  console.log(determineWinner(userChoice, computerChoice));
};
playGame();
[/codebyte]

1 Like

i didnt asked you to solve it i already solved 80% of it but i got stuck no matter what i write i get the your choice was undefined and the computer choose scissors so = undefined

1 Like

i just wanted a clue on what should i look deeper at it

1 Like

You are getting a result, so now trace it back to find out how/why you are getting that result. That’s what good debugging is all about.

And, yeah, that is what I meant by formatted raw code. Now just grab a pencil and follow the logic in your code.

1 Like

the results are = that this line
else if (userInput === "") { console.log("choose something");

and this line

 console.log(
    `your choice was ${userChoice} and the computer choose ${computerChoice} so = `
  );

are over-writing each other , but i need that one of them return null when the other one is bein executed

1 Like

so i only add
return null ?

1 Like