코딩테스트/HackerRank

HackerRank day2 - Loops

무지짱 2022. 12. 14. 14:28
반응형

처음에 문제를 잘못 이해해서

javascript 를 알파벳 순으로 출력하는건줄 알았는데

javascript 안에 a,e,i,o,u 를 출력하고 나머지 철자를 출력하는 것이었다.

 

먼저 vowel 변수에 a,e,i,o,u 배열을 담아주었고

for 문을 돌리며 vowel 이 포함되어있으면 출력,

그리고 나머지 알파벳을 출력하였다.

 

 

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });
    
    main();    
});

function readLine() {
    return inputString[currentLine++];
}

/*
 * Complete the vowelsAndConsonants function.
 * Print your output using 'console.log()'.
 */
function vowelsAndConsonants(s) {
    let vowels = ['a', 'e', 'i', 'o', 'u']
    
    for(let v of s){
        if(vowels.includes(v)){
            console.log(v)
        }
    }
    
       for(let v of s){
        if(!vowels.includes(v)){
            console.log(v)
        }
    }
}


function main() {
    const s = readLine();
    
    vowelsAndConsonants(s);
}
반응형