mardi 19 mai 2015

JavaScript: it works, but what is a more efficient way to write this function?

3 Mamma Chia Organic Energy Drinks, 2 ITO EN Jasmine Green Teas, 1 High Brew, 11:59PM in a brand new high-rise office and a deadline that was due at 10PM...

There has got to be a better way… But my brain is wrecked and I cannot see it now.

Enter a string into the input box below. If the string starts with:

"a" or "A" "starts with A" should be displayed. "b" or "B" "starts with B" should be displayed. "c" or "C" "starts with C" should be displayed. "d" or "D" "starts with D" should be displayed. "e" or "E" "starts with E" should be displayed. anything else "starts with something else" should be displayed.

function startsWith(input) {
    if(input==='a' || input==='A') {
        return ("starts with A");
    }
    else if(input==='b' || input==='B') {
        return ("starts with B");
    }
    else if(input==='c' || input==='C') {
        return ("starts with C");
    }
    else if(input==='d' || input==='D') {
        return ("starts with D");
    }
    else if(input==='e' || input==='E') {
        return ("starts with E");
    }
    else {
        return ("starts with something else");
    }
}

Aucun commentaire:

Enregistrer un commentaire