changing string cases without built in functions

 function upcase(string) {
    var up = '';

    for (var x = 0; x < string.length; x++) {
        if (string[x] >=97 && string[x]<=122)
            temp+= string[x] - 32;

        else if(string[x]>=65 && string[x]<=90)
            temp+=string[x] + 32;
    }
      return up;
}

Hi good evening i am currently working on a practice question that i am doing to prepare for an exam coming up. i am having problems with writing a function that will change the cases of a string from lower to uppercase. not sure what i should be changing or what i should add. any help would be appericated.

2 answers

  • answered 2018-04-17 04:22 rhythmbytes

    You could write a function for this like:

    function toUpper(string) {
    return string.split('').map(c => {
        switch(c) {
            case 'a': return 'A';
            case 'b': return 'B';
            case 'c': return 'C';
            case 'd': return 'D';
            case 'e': return 'E';
            case 'f': return 'F';
            case 'g': return 'G';
            case 'h': return 'H';
            case 'i': return 'I';
            case 'j': return 'J';
            case 'k': return 'K';
            case 'l': return 'L';
            case 'm': return 'M';
            case 'n': return 'N';
            case 'o': return 'O';
            case 'p': return 'P';
            case 'q': return 'Q';
            case 'r': return 'R';
            case 's': return 'S';
            case 't': return 'T';
            case 'u': return 'U';
            case 'v': return 'V';
            case 'w': return 'W';
            case 'x': return 'X';
            case 'y': return 'Y';
            case 'z': return 'Z';
            default: return c;
        }
    }).join('');
    

    All you need to do now is invoke it with your input string like this:

    toUpper('This iS a teSt');
    

    Which outputs:

    "THIS IS A TEST"

  • answered 2018-04-17 04:22 rhythmbytes

    A more brute force approach results in a function like:

    function toUpper(str) {
        let upperStr = '';
        for(let i = 0; i<str.length; i++) {
            switch(str[i]) {
                case 'a': upperStr+= 'A'; break;
                case 'b': upperStr+= 'B'; break;
                case 'c': upperStr+= 'C'; break;
                case 'd': upperStr+= 'D'; break;
                case 'e': upperStr+= 'E'; break;
                case 'f': upperStr+= 'F'; break;
                case 'g': upperStr+= 'G'; break;
                case 'h': upperStr+= 'H'; break;
                case 'i': upperStr+= 'I'; break;
                case 'j': upperStr+= 'J'; break;
                case 'k': upperStr+= 'K'; break;
                case 'l': upperStr+= 'L'; break;
                case 'm': upperStr+= 'M'; break;
                case 'n': upperStr+= 'N'; break;
                case 'o': upperStr+= 'O'; break;
                case 'p': upperStr+= 'P'; break;
                case 'q': upperStr+= 'Q'; break;
                case 'r': upperStr+= 'R'; break;
                case 's': upperStr+= 'S'; break;
                case 't': upperStr+= 'T'; break;
                case 'u': upperStr+= 'U'; break;
                case 'v': upperStr+= 'V'; break;
                case 'w': upperStr+= 'W'; break;
                case 'x': upperStr+= 'X'; break;
                case 'y': upperStr+= 'Y'; break;
                case 'z': upperStr+= 'Z'; break;
                default: upperStr+= str[i];
            }
        }
    
        return upperStr;
    }
    

    Invoking it with toUpper('This iS a teSt') results in the expected output "THIS IS A TEST"