관리 메뉴

Silver Library (Archived)

Conditional statements: switch 본문

F2. Problem & Solving/Solving

Conditional statements: switch

Ayin Kim 2021. 6. 12. 17:44
반응형

Personally, this algorithm is very useful & did not know how to do this thing.

According to other people, this can be better off.

 

function getLetter(s) {

    let letter;

    // Write your code here

    let characterA = "aeiou";

    let characterB = "bcdfg";

    let characterC = "hjklm";

    let characterD = "npqrstvwxyz";

    

    for(let i in s){

        if (characterA.includes(s[0])){

            letter = "A";

        } else if (characterB.includes(s[0])){

            letter = "B";

        } else if (characterC.includes(s[0])){

            letter = "C";

        } else if (characterD.includes(s[0])){

            letter = "D";

        }

    }

    

    

    return letter;

}

 

OR (include switch)

 

function getLetter(s) {

let letter;

// Write your code here

 

switch(true){

    case /^[aeiou]{1}.*/i.test(s):

        letter = "A"

        break;

    case /^[bcdfg]{1}.*/i.test(s):

        letter = "B"

        break;

    case /^[hjklm]{1}.*/i.test(s):

        letter = "C"

        break;

    case /^[npqrstvwxyz]{1}.*/i.test(s):

        letter = "D"

        break;

    }

return letter;

}

'F2. Problem & Solving > Solving' 카테고리의 다른 글

[JS] string includes() method  (0) 2021.07.06
Loops - JS algorithm  (0) 2021.06.12
Conditional statement: if-else  (0) 2021.06.12
[JS] Day 1 : let and const  (0) 2021.06.11
Plan of learning the algorithm  (0) 2021.05.01