I'm new to CodeCademy and I need help with JS Project

Hope I’m using this forum right. If not #sorry. I’m going thru JavaScript course and there is one project I struggle to finish. I know for many people this would be laughable but here I am.

Guess the easiest way if I put the code below:

const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();
  if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors')
  return userInput;
  else {
    console.log('Error');
  }
};
// UserInput works fine. Now to ComputerInput//
const getComputerChoice = (computerInput) => {
  Math.floor(Math.random() * 3)
  //Now to the choices of the computer//
  if (computerInput === 0)
    return 'rock'
  else if (computerInput === 1)
    return 'paper'
  else if (computerInput === 2)
    return 'scissors'
  else console.log('Something went horrebly wrong. Look in code line 10-20 in rockPaperScissors.js')
};
/*Still works perfectly. Gave the parameter 'computerInput' even said to leave empty. Why?*/
//Now it is the time to determine a winner #7//
const determineWinner = (userChoice, computerChoice) => {
  if (userChoice === computerChoice) 
    return 'The game is a tie!';
  if (userChoice === 'rock')
    if(computerChoice === 'paper')
      return 'computer won'
  if (userChoice === 'paper')
    if(computerChoice === 'rock')
      return 'you won'
  if (userChoice === 'rock')
    if(computerChoice === 'scissors')
      return 'you won'
  if (userChoice === 'scissors')
    if(computerChoice === 'rock')
      return 'computer won'
  if (userChoice === 'scissors')
    if(computerChoice === 'papers')
      return 'you won'
  if (userChoice === 'paper')
    if(computerChoice === 'scissors')
      return 'computer won'
}
  
  
  //console.log(determineWinner('paper', 'scissors')) // <== checking determineWinner//

//Start the game #12//
const playGame = () => {
  const userChoice = getUserChoice('scissors');
  const computerChoice = getComputerChoice();
  console.log('You threw: ' + userChoice);
  console.log('The computer threw:' + computerChoice);
  console.log(determineWinner(userChoice, computerChoice));
};

playGame();

Hope somebody can help me there. This is also part of trying out the forum. Opened for critique

Hello @waniamarshall. Welcome to the forum.
It will be much easier to help you, if you will post your code using the </> button located in the menu bar above where you type your post. If you will place your cursor on a blank line, then click the </> button, and finally paste your code in the space indicated, your code will retain its original formatting.
Like this:

I chose the above function for my example because this is where your problems begin. I indented your code as if I had typed it orginally.

First, this function should not take a parameter:
const getComputerChoice = (computerInput) => { should be changed to
const getComputerChoice = () => {.
Second, since you use the variable computerInput in your if and else if statements, and computerInput isn’t being assigned a value as a parameter, it needs to be assigned somewhere. This would be the perfect spot: Math.floor(Math.random() * 3). Otherwise, this line accomplishes nothing, A random int (0, 1 or 2) is generated, but not saved or assigned to anything. It just disappears.
Third, consistency with the use of ;'s and { }'s will go a long way toward making your code more readable. Here is the same snippet of your code with the ;'s and { }'s in place (also added some comments):

const getComputerChoice = (computerInput) => { //there should be no parameter
  Math.floor(Math.random() * 3); //need to assign computerInput here
//Now to the choices of the computer//
  if (computerInput === 0) { //opening brace
    return 'rock'; //semi-colon
  } //closing brace
  else if (computerInput === 1) {
    return 'paper';
  }
  else if (computerInput === 2) { //if it weren't for the else below, a simple else would suffice here since it's the only option left
    return 'scissors';
  }
  //looks like you added the else for your own debugging purposes. Not a bad idea, but remember to delete it when you're finished debugging.
  else {
    console.log('Something went horrebly wrong. Look in code line 10-20 in rockPaperScissors.js');
  }
};

Hopefully this is enough to get you started. There are additional problems with your determineWinner() function, but we can look at that after fixing the getComputerChoice() function.

4 Likes

I completed this last week sometime. It took me forever. It really helps to do your work in a text editor that can help you check syntax errors. So, here is what I got:

Here is my answer

This text will be hidden

const getUserChoice = (userInput) => {
    userInput = userInput.toLowerCase();
      if (userInput === 'rock' || userInput === 'scissors' || userInput === 'paper' || userInput === 'bomb')
      {
        return userInput;
      }
    else { 
      console.log ('Error!');
            }
    };
  
  const getComputerChoice = () => 
  {
    const randomNumber = 
          Math.floor(Math.random() * 3);
    switch (randomNumber)
    {
      case 0:
        return 'rock';
      case 1:
        return 'paper';
      case 2:
        return 'scissors';
    }
  }
  
  const determineWinner = (userChoice, computerChoice) => { if(userChoice === 'bomb'){
    return 'You destroyed the Computer'
  }
                                                          
  
    if (userChoice === computerChoice){
      return 'This game is a Tie';
    }
  
  if (userChoice === 'rock'){
    if (computerChoice === 'paper')
    {
      return 'The Computer Won!';
    }
        else {return 'The User Won!';
    }
  }  
    
  if (userChoice === 'paper'){
    if (computerChoice === 'scissors')
    {
      return 'The Computer Won!';
    }
    
      else return 'The User Won!';
  }
    
  
  if (userChoice === 'scissors'){
    if (computerChoice === 'rock'){
      return 'The Computer Won!';
        }
    
      else return 'The User Won!';
      
    }
  }
  
  const playGame = () =>{
    const userChoice = 
      getUserChoice('bomb');
    const computerChoice = 
      getComputerChoice ();
    console.log("Your choice is: " + userChoice);
    console.log("The computer choice is: " + computerChoice);
    console.log(determineWinner(userChoice, computerChoice));
  }
    playGame();
  

I know its long. Hope it can help you out though.:+1:

6 Likes

Hello, @wickdw8yz. While your willingness to help a fellow learner is admirable and appreciated, posting your solution to the exercise isn’t the best way to help them learn. If you wouldn’t mind editing your post by highlighting the code, then clicking on the ‘gear’ icon on the far right of the menu bar, and then selecting either the Hide Details or Blur Spoiler option to hide your solution from view unless the viewer clicks on it, I would appreciate it.

Examples:

Spoiler
console.log('Hello, world!');

Or:
Spoiler:

console.log('Hello, world!');
6 Likes

Hey, @midlindner. Sorry. I was just trying to be helpful. I’m new to all of this stuff. But, I see where you are coming from with this. So, any guidance given is appreciated. I had never used the </> til earlier and i haven’t used this hidden feature til now. It says its hidden, but it doesn’t show it to me that way. Thanks for the input.

5 Likes

It doesn’t appear to be hidden.
If you used Hide Details:

[details="Summary"] //you can change "Summary" to whatever title you like
This text will be hidden //your code should be between the [details][/details] tags.
[/details]

If you used Blur Spoiler:

[spoiler]
#your code should be between the [spoiler][/spoiler] tags
[/spoiler]

Also, no need to apologize. We were all new at one time. I started out in a similar fashion. Keep up the good work!

1 Like

Thank you for the info. I got it worked out now, I think.

3 Likes

That’s perfect! Thanks! It’s also good to include some explanation of how you came to your solution along with tips to help the original poster find a solution. Not saying you need to do that here. Just food for thought. Again, your willingness to help is a big plus here on the forum :+1:

5 Likes

Thank you very much!

1 Like
// Thanks for looking over this mess. I'm learning :) //
const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();
  if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors')
  return userInput;
  else {
    console.log('Error');
  }
};
const getComputerChoice = () => {
  Math.floor(Math.random(computerInput) * 3)
  /*Don't really get why ^^^^ this one should be empty. And did I used computerInput this time right? I would put the whole math.floor inside the computerInput() but the color doesn't responded so I thought wrong*/
  if (computerInput === 0) {
    return 'rock';
  }  
  else if (computerInput === 1) {
    return 'paper';
  }  
  else (computerInput === 2) {
    return 'scissors';
  }  
}
/*^^ braces and semi-colon fixed.(I hope). Also removed the else for bebugging(Didn't really knew its purpose)*/
const determineWinner = (userChoice, computerChoice) => {
  if (userChoice === computerChoice) {
    return 'The game is a tie!';
  }
  if (userChoice === 'rock') {
    if(computerChoice === 'paper') {
      return 'computer won';
    }
  }  
  /*^^^^^^^^@midlinder Do I really have to use braces inside the braces? Would make sense but kinda to much idk*/
  if (userChoice === 'paper')
    if(computerChoice === 'rock')
      return 'you won'
  if (userChoice === 'rock')
    if(computerChoice === 'scissors')
      return 'you won'
  if (userChoice === 'scissors')
    if(computerChoice === 'rock')
      return 'computer won'
  if (userChoice === 'scissors')
    if(computerChoice === 'papers')
      return 'you won'
  if (userChoice === 'paper')
    if(computerChoice === 'scissors')
      return 'computer won'
}
const playGame = () => {
  const userChoice = getUserChoice('scissors');
  const computerChoice = getComputerChoice();
  console.log('You threw: ' + userChoice);
  console.log('The computer threw:' + computerChoice);
  console.log(determineWinner(userChoice, computerChoice));
};

playGame();

Have to? No. You don’t, but using them around your blocks of code follows convention. In this determineWinner() function, regardless of curly braces, the logic should be cleaned up a little. After each selection is made, there are basically only 4 comparisons required.
Your first one is perfect:

  if (userChoice === computerChoice) { //First, is it a tie? 
    return 'The game is a tie!';
  }

Now, since you’ve dealt with every possible tie, there are only a few possibilities left.
If the human chose rock did the computer chose paper? If yes, then the computer won. If no, then the human won. Therefore, you only need three additional if statements (one for each option: rock, paper or scissors). Each if will need an else, and you’ll have everything covered.

2 Likes

Thanks for your effort!

2 Likes

You’re very welcome! Happy coding!

1 Like