LEARN JAVASCRIPT: FUNCTIONS Rock, Paper, or Scissors Task #4


#1

I got 1,2 & 3, I am on #4 and I wanted to test it

I input the code the way the hint tells me to and I get a syntax error. I cant figure out where I went wrong. Even when I go back to #3 which the system tells me I did correctly. When I input the console.logs to Test the function by calling it with valid and invalid input I get an error. Not sure what I am doing wrong, the check boxes were being checked off until I got to #4

const getUserChoice = (userInput) = > {
userInput=userInput.toLowerCase();
if (userInput === ‘rock’ || userInput === ‘paper’ || userInput === ‘scissors’){
return userInput;
} else {
console.log ('error ');
}
console.log(getUserChoice(‘Paper’));
console.log(getUserChoice(‘fork’));
};

****I WANT TO SAVE FUNCTION TO CONSOLE AND CALL IT WITH CONSOLE.LOG AND TEST IT WITH VALID / INVALID RESPONSE, Getting syntax error when try to save to the console


#2

Move those two lines outside of the function so they are in global scope and above to call the function.


#3

if there above repl.it console tells me getUserChoice was used before it was declared, if after I still get syntax error


#4

Move the final closing brace to above those two lines so they can execute freely.


#5

yep still getting error below is updated code still erroring out with syntax error.

const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();
  if (userInput === ‘rock’ || userInput === ‘paper’ || userInput === ‘scissors’) {
    return userInput;
  } else {
    console.log ('error ');
  }
};

console.log(getUserChoice(‘Paper’));
console.log(getUserChoice(‘fork’));

#6

I did a couple of tweaks to your code, the most important being the arrow function

= >  must be writtern as, =>

Some indentation helps us to see the nesting. It may have been lost when you copied the code to the post editor.

When passing only a single parameter to an arrow function, we do not need the parens.


Now our question would be, what do the instructions say to do with an error in the inputs? Log, or return? Please post a link to the exercise so we can test your code.


#8

thanks but still something off I cant see what it is.

link to exercise attached.
https://www.codecademy.com/courses/learn-javascript-■■■■■■■■■■■■■■■■■■■■■■■-paper-scissors?course_redirect=introduction-to-javascript

Instructions are as follows
4)Test the function by calling it with valid and invalid input, and printing the results to the console.

You can delete this when you know your function works.
*****My function dosent work when I log ‘paper’ to the console even though it should log ‘paper’ to the console along with ‘rock’ & ‘scissors’ it is not doing that . so something is off.


#9

correct resonse in console should be:
paper
error

I am not getting that it is telling me Paper is not defined at Object


#10

Please post the code you have at present so we can test the latest updated version.


#11

current code:

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

console.log(getUserChoice(paper));


#12

Remember to pass a string object, not an identifier. paper is not defined.


#13

thankxIT WORKS!!! YOUR A GUINNESS!!!


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

console.log(getUserChoice('paper'));
console.log(getUserChoice('flamethrower'));

#15

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