프로그래밍/알고리즘 풀이
[node.js] 문자열 분석 ( 백준 10820번 )
카카수(kakasoo)
2021. 4. 15. 13:11
반응형
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.on("line", (line) => {
main(line);
});
/**
*
* @param {string} line
*/
const main = (line) => {
let small = 0;
let big = 0;
let num = 0;
let space = 0;
for (let i = 0; i < line.length; i++) {
const cur = line[i];
if (!isNaN(cur - 0)) {
if (cur === " ") {
space++;
} else {
num++;
}
}
if (
"a".charCodeAt(0) <= cur.charCodeAt(0) &&
cur.charCodeAt(0) <= "z".charCodeAt(0)
) {
small++;
}
if (
"A".charCodeAt(0) <= cur.charCodeAt(0) &&
cur.charCodeAt(0) <= "Z".charCodeAt(0)
) {
big++;
}
}
console.log(`${small} ${big} ${num} ${space}`);
};
isNaN은 파라미터가 없으면 true를 뱉는다. 사실 당연하다. 보통 다 그렇게 구현하지 않을까?
boolean값을 만들어놓고, 조건을 통과하지 못하면 false로 바꾸는데, 파라미터가 없으면 조건 자체를 못 타니깐.
반응형