Need help with Rock Paper Scissors task


#1

Hello world!

I’ve been struggling with this task all day yesterday, even restarted it completely today but still I get the same result.
https://www.codecademy.com/courses/learn-javascript/projects/rock-paper-scissors?action=resume_content_item

So, here’s what I have now:

var userChoice = ''
userChoice = 'Rock'
userChoice = userChoice.toLowerCase();

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

function determineWinner(userChoice,computerChoice){
  if (userChoice === computerChoice) {
  	return 'It\'s a tie!'
  }
  else {
    return 'Not a tie.'
  }
}

console.log('User plays '+userChoice+'.');
console.log('Computer plays '+computerChoice()+'.');
console.log(determineWinner());
console.log(determineWinner(userChoice,computerChoice));

User plays rock.
Computer plays paper.
It’s a tie!
undefined

Every time it returns “It’s a tie”. I’ve been tinkering with it and it looks like determineWinner function doesn’t recognize userChoice and computerChoice at all, so it gives me “It’s a tie” because both arguments are undetermined.


#2

You only have an output for a tie in the function. Try adding a losing and winning output. The computer doesn’t know what to do if it isn’t a tie. Hope this helps!


#3

Oh, I forgot to mention. I’ve added else “not a tie” just to test it out and it still gives me the same result. As I was saying, it looks like determineWinner function doesn’t recognize the arguments. Also, in theory it should recognize those arguments but I haven’t found any examples of another function being an argument. And if I write

function determineWinner(userChoice,computerChoice())

(brackets after computerChoice function) it gives me an error pointing at those brackets.
It’s a bit confusing because I still can’t get in which cases I should specify arguments and in which I shouldn’t.


#4

So you are just trying something out and not solving the exersise. Ok get it.

You gotta add a “break” after your switch.

ex:


function yourFunction() {
  switch (input) {
    case 0:
      return "rock";
      break;
    case 1:
      return "paper";
      break;
    etc etc......
  }
}

#5

Ok, thank you. I’ll try that.
Well I am trying to solve solve the exercise but I’m doing it step by step to make sure that I fuly understand every little detail of the code that I write.


#7

…that still didn’t help. I keep getting the same result.


#8

Nooooooooooo! Yeah im out of ideas. Sorry!


#9

Thanks for trying, bro =)


#10

break is unreachable after return. Don’t let your future employer see this or that job could be history.

case is not a function so does not need parens on the expression. That part of the above post is correct.


#11

If you’ve determined that you’re passing in undefined as arguments to your function then you’d look at where you got that from wouldn’t you?

And since it’s your program there’s nothing stopping you from writing to screen what is being done.

There’s no mystery here is there? Narrow it down until you have found the point where something goes wrong*, fix it.

*wrong being a difference in expectation and observed outcome


#12

Thanks. Been googling examples here and have actually changed it right before you mentioned :smiley:
That still doesn’t solve the problem. And in fact, that didn’t change the result at all, just made my code a bit more tidy, I guess.


#13

I’m guessing you’ve got past this point by now?


#14

And this is exactly why I made this post. I’m seeing that a function doesn’t recognize provided arguments. I’ve been tinkering with it and have tried a few ways of writing it down and none of them work. At least this one doesn’t give me a syntax error.


#15

No-no. I’m still stuck at the program giving me “It’s a tie” no matter the arguments.


#16

There’s no recognition going on though. There’s only what you have written.

Generally you go about narrowing things down like this:

At the start of the program everything is correct, because nothing has happened and nothing should have happened.

At the end you have the wrong result.

Next, find a place somewhere in the middle of that. Is it wrong at that point? If so, look at an earlier point, otherwise, look at a later one.

Repeat until found.

(Same thing as looking up a phone number in a (paper version) phone directory)


#17

You do realize that if we are passing a function (ie function return value) to another function, it has to be done in the argument, not the parameter?


#18

Ok… can you give me an example? I don’t seem to get that right.


#19

That needs to be computed in the call expression.


#20

I had that logic as well but here’s what I get:

/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:17
function determineWinner(userChoice,computerChoice()){
^
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)

(It is pointing at those brackets)


#21

That’s because of the extra parens in the parameter. There should only be two identifiers in the parameter list. No expressions allowed, here.

function foo(x) {
    return typeof(x)
}
function bar(x) {
    return x + x
}
console.log(bar('bar'), foo(bar('bar')));
barbar string
console.log(bar(21), foo(bar(21)))};
42 "number"