티스토리 뷰

728x90

아주 기초적인 cli 프로그램을 만들었다.

1 단계

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
const readline = require('readline') ;
 
const rl = readline.createInterface({
    input: process.stdin, 
    output: process.stdout,
}) ;
 
rl.question('Node.js 재밌나요 ? (y/n) ', (answer) => {
    if (answer === 'y') {
        console.log('신나는 node!!') ;
    } else if (answer ==='n') {
        console.log('재미없는 node@! ~') ;
    } else {
        console.log('y 또는 n 만 입력하세요 ') ;
    }
    rl.close() ;
}) ;
 
 

문제 1 ) 이렇게 코드를 짜게 된다면 콘솔창이 깨끗하게 지워지지 않고,

문제 1) 다른 것을 입력했을 때(else구문) 프로그램이 꺼지게 된다.

 

해결

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
const readline = require('readline') ;
 
const rl = readline.createInterface({
    input: process.stdin, 
    output: process.stdout,
}) ;
 
console.clear() ;
 
const answerCallback = (answer) => {
    if (answer === 'y') {
        console.log('신나는 node!!') ;
        rl.close() ;
    } else if (answer ==='n') {
        console.log('재미없는 node@! ~') ;
        rl.close() ;
    } else {
        console.clear() ;
        console.log('y 또는 n 만 입력하세요 ') ;
        rl.question('노드가 재밌습니까? (y/n)', answerCallback) ;
    }
} ;
 
rl.question('Node.js 재밌습니까 ? (y/n) : ', answerCallback) ;
 

 

해결 1) console.clear()를 대답이 끝날때 넣어줘 console을 지워줌

해결 2) 재귀함수를 사용하였다.