Why my conditionals don't work


#1
const getUserChoice = (userInput) => {
  userInput = userInput.toLowerCase();
  if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') {
    return userInput;
  } else {
    console.log('Invalid input');
  }
};

const getComputerChoice = () => {
  const compChoice = Math.floor(Math.random() * 3);
  if (compChoice === 0) {
    return 'rock';
  } else if (compChoice === 1 ) {
    return 'paper';
  } else {
    return 'scissors';
  }
};

const determineWinner = (userChoice, computerChoice) => {
  if (userChoice === computerChoice) {
    return 'It\'s a tie';
  } else if (userChoice === 'rock' && computerChoice === 'scissors') {
    return 'Player wins!';
  } else if (userChoice === 'paper' && computerChoice === 'rock') {
    return 'Player wins!';
  } else if (userChoice === 'scissors' && computerChoice === 'paper') {
    return 'Player wins!';
  } else {
    return 'Computer wins!';
  }
};

console.log(determineWinner(getUserChoice('rock')), getComputerChoice());

The issue here is with the conditional determineWinner(). Why is it only returning “Computer wins!”? The others should be getting evaluated. Is my logic wrong?


#2

computerChoice is undefined:

const determineWinner = (userChoice, computerChoice) => {
  console.log(computerChoice);

so then your focus should shift here:

console.log(determineWinner(getUserChoice('rock')), getComputerChoice());

take a minute to look at it, fiddle with it. Let me know if you need more help


#3

Sorry, now I know what’s wrong. What a dumb mistake.

If you want you can delete this topic.


#4

Good, that was my intention :slight_smile:

if dumb mistakes where not made, software development would go a lot faster.

i hope you can also learn from what i did here, you had a condition which was failing, so the next logic thinking step should be checking the values


#5

You mean the parentheses mistake was not the only one? I made a logic mistake in my conditionals as well? I mean, did check the output after fixing that mistake and everything seems to be fine.


#6

the parentheses was the only mistake, but i hope you also saw how i debugged the issue, or more generally, how you should go about fixing bugs. To improve your own debug skills

the condition was failing because of the wrong parentheses.


#7

I understand now. I thought about copying the code to VS Code but I was so certain it was a logic mistake that it didn’t even cross my mind that it could be a syntax issue. I kid you not, I spent like, almost an hour trying to find what was wrong before I posted here. I just couldn’t see it and if you hadn’t pointed out I probably wouldn’t have figured out.

I appreciate your help and I’m very happy by how friendly and patient you and the other mods here are with these beginner “dumb” questions/mistakes. You guys do a great job.

I’m glad I posted this here instead of Stack Overflow. A beginner quickly learns of the ruthlessness of that place. I’m still recovering from my last humiliation.


#8

never assume, always verify.

yep, but the questions on this forum restrict to lesson questions (mostly), so even if the topic is of lesser quality, its less problematic then on stackoverflow given we might figure out the missing detail by visiting the lesson.

well, people who hijack a topic and only post something like: i am also stuck here please help me, are difficult to deal with. Or people who want you to code the solution and do not take kindly on directions only (lucky that are very few)