Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

275
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda