0

Is there a more efficient/ concise/ eloquent way to write this:

else if ((aInd === 3)||(aInd === 7)||(aInd === 9)||(aInd === 19)
     letter = alphabet[aInd + 1].toUpperCase();

Is there any valid syntax that looks like

if (a === (3 || 7 || 13 || 19) {do this}

... or lets you group sets of values of for a single variable in a more concise way than a bunch of boolean expressions with || logic?

Background: Novice programmer, working through a basic cypher challenge on coderbyte that requires taking a certain action (capitalizing) if the letter is a vowel. For reasons of simplicity (because it's used elsewhere in the program) I declared an array alphabet containing each letter a-z in the form of a string. The 'cypher' also transposes every letter one to the right, so the alphabet indexes of the letters to become vowels are 3, 7, 13, and 19. aInd represents the index of letter in the alphabet.

Full code:

function letterChanges(str) {
    var alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
    var words = str.toLowerCase().split(' '), senArray = words.map(function(word) {return word.split('')});
    senArray.forEach(function(word) {
        word = word.filter(function(letter, index, word) {
            if ((Boolean(alphabet.indexOf(letter))) === false) return false;
            else return true;
        });
    });

    senArray[0].map(function(letter) {
        var aInd = alphabet.indexOf(letter);
        switch (aInd) {
            case 25: 
                letter = alphabet[aInd].toUpperCase();
                break;
            case (3 || 7 || 13 || 19):
                letter  = alphabet[aInd + 1].toUpperCase();
                console.log(letter);
                break;
            default:
                letter  = alphabet[aInd + 1];
                break;
        }
        return letter;
    })
    
    return senArray.map(function(word) {
        return word.map(function(letter) {
        var aInd = alphabet.indexOf(letter);
        if (aInd === 25) {letter = alphabet[aInd].toUpperCase();}
        else if ((aInd === 3)||(aInd === 7)||(aInd === 13)||(aInd === 19)) {letter  = alphabet[aInd + 1].toUpperCase();}
        else {letter  = alphabet[aInd + 1];}
        return letter;
    }).join('');
    }).join(' ');
}

letterChanges("Input string goes here.");
JCOC611
  • 19,111
  • 14
  • 69
  • 90
Alex Patch
  • 125
  • 1
  • 9
  • Just noticed I missed a ) in the top example. It should read: if (a === (3 || 7 || 13 || 19)) {do this}. Oops. – Alex Patch Nov 25 '15 at 18:21

1 Answers1

0

Pass the values as an array and check the indexOf.

In your case,

if ([3, 7, 13, 19].indexOf(aInd) > -1) {
  letter  = alphabet[aInd + 1].toUpperCase();
}
Charlie
  • 22,886
  • 11
  • 59
  • 90
  • I was trying to do this with a switch statement, which would take multiple cases and execute the same thing, but I don't think that's possible as I was doing it. This is a good alternative approach. – Alex Patch May 06 '16 at 22:47