vendredi 28 juin 2019

The syntax for checking equality of a variable

Want to know if there is an option to optimize (to make it more competently) the syntaxes of the comparison

I have a simple code which checks the inputs to my function through the "if - else" conditional statements. I verify if the variable equals to one of the inputs or not using logical "OR" operator.


function cc(card) {
  // Only change code below this line
  if (card==2 || card==3 || card==4 || card==5 || card==6) {
    count+=1;
  }
  else if (card==7 || card==8 || card==9) {
count+=0;
  }
  else if (card==10 || card=="J" || card=="Q" || card=="K" || card=="A") {
    count-=1;
  }
  else {
  return "No such combination";
  }
  if (count>0) {
    return count + " " + "Bet";
  }
  else {
    return count + " " + "Hold";
  }
  // Only change code above this line
}

// Add/remove calls to test your function.
// Note: Only the last will display
cc(7); cc(8); cc(9);

I would like to know if I can replace this amount of "OR" operators with another syntaxes? I know about "switch" method, but now I'm interested in particular this approach.

Thank you!

Aucun commentaire:

Enregistrer un commentaire