JavaScript 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);
}
Answer:1

One option is to use an array of characters, then use .indexOf to find the index of the character:

const word = 'bBac';
const chars = ['a', 'b', 'c', 'd'];

const y = [...word].map(char => chars.indexOf(char.toLowerCase()))
console.log(y);
// return y;
Answer:2

In my app I have several classes which are used to create XML strings. Each class has a few methods that take some arguments and return a string. I want to specify this limitation so that methods with ...

In my app I have several classes which are used to create XML strings. Each class has a few methods that take some arguments and return a string. I want to specify this limitation so that methods with ...

In my SPA I am using axios to make requests to the API. I currently use axios request interceptors to add auth headers to requests but I also wanted to use them to catch 401 errors and remove the ...

In my SPA I am using axios to make requests to the API. I currently use axios request interceptors to add auth headers to requests but I also wanted to use them to catch 401 errors and remove the ...

  1. axios catch request

I need regular expression to not allow only whitespaces in a field(the user should not enter just whitespaces in the field) but it can allow completely empty field and it can also allow string with ...

I need regular expression to not allow only whitespaces in a field(the user should not enter just whitespaces in the field) but it can allow completely empty field and it can also allow string with ...

  1. regular expression javascript tester
  2. regular expression javascript example
  3. regular expression javascript tutorial
  4. regular expression javascript test
  5. regular expression javascript phone number
  6. regular expression javascript mdn
  7. regular expression javascript generator
  8. regular expression javascript match
  9. regular expression javascript email
  10. regular expression javascript w
  11. regular expression javascript or
  12. regular expression javascript phone number validation
  13. regular expression javascript replace
  14. regular expression javascript space
  15. regular expression javascript validation
  16. regular expression javascript special characters
  17. regular expression javascript escape characters
  18. regular expression javascript for name
  19. regular expression javascript numbers only
  20. regular expression javascript for password

When it comes to custom extended element methods such as PlainJS closest() DOM Element method: // closest polyfill this.Element && function(ElementPrototype) { ElementPrototype.closest = ...

When it comes to custom extended element methods such as PlainJS closest() DOM Element method: // closest polyfill this.Element && function(ElementPrototype) { ElementPrototype.closest = ...