mardi 4 décembre 2018

JavaScript: Condensing if else statement

Is there a shorter more efficient way of doing this? it seems a little hefty and I just want to know if it can be condensed?

 var y = []

  for(let i=0;i < word.length;++i){
    if(word[i] == "A"|| word[i] == "a"){
      y.push(0)
    }
    else if(word[i] == "B"|| word[i] == "b"){
      y.push(1);
    }
    else if(word[i] == "C"|| word[i] == "c"){
      y.push(2);
    }
    else if(word[i] == "D"|| word[i] == "d"){
      y.push(3);
    }
and so on..


  return(y);
}

Aucun commentaire:

Enregistrer un commentaire