Need help with Rock Paper Scissors task

Hello world!

I’ve been struggling with this task all day yesterday, even restarted it completely today but still I get the same result.

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()+'.');

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

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.

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!

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.

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

You gotta add a “break” after your switch.


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

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.

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

Nooooooooooo! Yeah im out of ideas. Sorry!

Thanks for trying, bro =)

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.

1 Like

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

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.

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

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.

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

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)

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?

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

That needs to be computed in the call expression.

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

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)

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"