Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

268
Views
Function "undefined" message in Rock, Paper, Scissors game in Codecademy

This function is supposed to produce exactly 'rock', 'paper', or 'scissors' (input is not case sensitive). But when I put in the wrong value (ie. 'papers' with an extra 's'), my function prints the error message (expected) AND a message saying "undefined" (not expected).

const getUserChoice = (userInput) => {
  userInput = userInput.toLowerCase();
  if (userInput === 'rock' ||
      userInput === 'paper' ||
      userInput === 'scissors') {
        return userInput;
      } else {
        console.log('Please enter rock, paper, or scissors.')
      }
}
console.log(getUserChoice('papers')); #prints 'paper' correctly if paper is the input

EDIT: I updated the question to be clearer and I already found the answer. It has to do with the difference between console.log and return.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

It's because your console.loging the error message rather then returning it

and when it ask you to 'Please enter rock, paper, or scissors' your not returning anything and thus the return is undefined.

so switch

console.log('Please enter rock, paper, or scissors')

to

return 'Please enter rock, paper, or scissors';


about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!