My code always returns 'The result is a tie!'


#1

Hey there,

I'm trying to do a bit of this exercise on my own, and the main thing I'm doing differently is creating else if lines rather than if statements inside more if statements. Is this a viable alternative? Could this be the reason my code is only returning 'The result is a tie" for the final line console.log(determineWinner(); ?

Here is what the console shows in one outcome:

The user has chosen: rock
The computer has chosen: scissors
The result is a tie!


function getUserChoice () {
  var userInput = prompt('Rock, paper, or scissors?');
  userInput = userInput.toLowerCase();
  if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') {
    return userInput;
  } else {
  return 'Error!';
  } 
}
console.log('The user has chosen: ' + getUserChoice());

function getComputerChoice () {
  var randomNumber = Math.floor(Math.random() * 3);
  switch (randomNumber) {
    case 0:
      return 'rock';
    case 1:
      return 'paper';
    case 2:
      return 'scissors';
  }
}
console.log('The computer has chosen: ' + getComputerChoice());

function determineWinner (userChoice, getComputerChoice) {
  if (userChoice === getComputerChoice) {
  return 'The result is a tie!';
  } else if (userChoice === 'rock') {
    if (getComputerChoice === 'paper') {
      return 'Computer wins!';
    } else {
      return 'You win!';
    }
  } else if (userChoice === 'paper') {
    if (getComputerChoice === 'scissors') {
      return 'Computer wins!';
    } else {
      return 'You win!';
    }
  } else if (userChoice === 'scissors') {
    if (getComputerChoice === 'paper') {
      return 'Computer wins!';
    } else {
      return 'You win!';
    }
  } 
}

console.log(determineWinner());


#2

function determineWinner (userChoice, getComputerChoice) {
  console.log(arguments[0], arguments[1]);
  ...
}

Output

The user has chosen: rock
The computer has chosen: scissors
undefined undefined
The result is a tie!

Notice the undefined, twice? That why it is always a tie.

getComputerChoice is actually a string copy of the function, not a value.

Your code should store the two inputs in discrete variables. If they exist in global scope, then no need for parameters. The function can see them.

Otherwise, pass the globals, but set the parameters to discrete local variables, such as user, computer.

function getComputerChoice () {
  var randomNumber = Math.floor(Math.random() * 3);
  switch (randomNumber) {
    case 0:
      return 'rock';
    case 1:
      return 'paper';
    case 2:
      return 'scissors';
  }
}

This is a lot of code to do essentially this:

function getComputerChoice() {
  return ['rock', 'paper', 'scissors'][Math.floor(Math.random() * 3)];
}

#3

Hey mtf,

Thanks for the reply. I've been trying to understand it, but I think my knowledge of all of these terms is lacking. I'm going to start the course once again and use this reply as a reference when going through this project again.

This is really cool, I'll keep that in mind. I haven't come across it in any of the JavaScript lessons thus far. Had no idea it could be done that way.


#4

It's important that we follow the full blown simple structures until we get a clear idea of how they work. Fundamentals are critical and this is the time to go over them as much as you need, before going forward.

The above is refactored, and not the sort of code I would suggest to a beginner, just something to look toward as skills improve and the knowledge base expands. Nobody is expected to write code like this in any of the track courses.

Consider this form of the code:

function getComputerChoice() {
    var choices = ['rock', 'paper', 'scissors'];
    var choice = Math.floor(Math.random() * 3);
    return choices[choice];
}

This is actually more readable, and understandable. There is no sin in writing code this way. None at all. If you gave me this code I would say it is good. If you gave me the other code, I would ask where you found it. (I am suspicious of anything too advanced in these forums.)

Something else for future reference, caching. If we have a program that calls on the Math module repeatedly, we can benefit from caching the methods that our program uses, random() and floor() for instance.

At the beginning of the program (not the function),

var random = Math.random;
var floor = Math.floor;

Note that we do not invoke them. We merely cache the string representation in a defined global variable for invoking later. Now our code no longer needs to call the Math module.

function getComputerChoice() {
    return ['rock', 'paper', 'scissors'][floor(random() * 3)];
}

#5

Yeah I'm finding that reviewing sections each day is more beneficial right now rather than trying to get through new material constantly. I've been going between the JavaScript and Learn JavaScript courses to see which works better for me. Learn JavaScript seems more user friendly but I do enjoy the depth of the other.

Again I really appreciate the responses, and I'll be reading into this further after a bit of review.


#6

I'll close by saying that there is a lot of material to cover yet, and it won't hurt to keep going, as long as you review as you go, and loop back to earlier lessons until you have them cemented. By going forward, you are getting a chance to use what you've learned so far, along with what new concepts are being introduced.

This project is a good example of one to come back to at the end of the track. You will have a totally different perspective and be able to build upon and improve the whole game.


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.