mardi 10 août 2021

I'm new to coding and I'm trying to wrap my head around using multiple || (or) in if statemtent conditions in javascript [duplicate]

const getUserChoice = userInput => { 
   userInput = userInput.toLowerCase();
   // I've also tried using '===' instead of '=='
   if (userInput == 'rock' || 'paper' || 'scissors') return console.log('great')
   else return console.log('Retype Input');
 };
// This is suppose to return 'Retype Input' but instead returns 'great'
getUserChoice('get');


// This is how I got it to work as intended
 const getUserChoice = userInput => { 
   userInput = userInput.toLowerCase();
   return userInput == 'rock' ? console.log('Great!')
   : userInput == 'paper' ? console.log('Great!')
   : userInput == 'scissors' ? console.log('Great!')
   : console.log('Retype Selection');
 };
getUserChoice('rock');

Is it possible to make it work like my first attempt (when it comes to if statements) or do I have to write individual ifs for each variable I want to check? And what would be an efficient way to refactor or decompose this code?

Aucun commentaire:

Enregistrer un commentaire