관리 메뉴

Silver Library (Archived)

JS, for 문 - 백준 2739: 구구단 본문

CS Library/JavaScript - Data Structure

JS, for 문 - 백준 2739: 구구단

Chesed Kim 2021. 7. 20. 21:37
반응형

문제

N을 입력받은 뒤, 구구단 N단을 출력하는 프로그램을 작성하시오. 출력 형식에 맞춰서 출력하면 된다.

입력

첫째 줄에 N이 주어진다. N은 1보다 크거나 같고, 9보다 작거나 같다.

출력

출력형식과 같게 N*1부터 N*9까지 출력한다.


- 예제 입력 1 에서, 주어지는 값은 '2' 다.

- 예제 출력 1 에서 주어지는 값들은 다음과 같다.

 

2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18

그럼, 답은 어떨까.

const readline = require('readline');

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

let input = [];
//
let fixedPoint = 0;
//
rl.on("line", function (line) {
    input = line.split(" ").map((_) => Number(_));
}).on("close", function () {
    fixedPoint = input[0]
    
    for (let i =1; i <= 9; i++) {
        console.log(`${fixedPoint} * ${i} = ${fixedPoint * i}`);
    }
    process.exit();
})

노트.

Node.js Module 에 대해 좀 더 알아 보면서 알 수 있었던 게 보인다.

 

절대 틀릴 일 없겠지만, 틀릴 수도 있으니 보험!

조심해야 할 점. 위의 "close" 는 행여라도 착각하면 안된다.

rl.on 처럼, rl.close() 로 method 로 사용 되면 close() method 로서 작용하게 된다.

위 예시 속 "close" 는 그냥 부여 된 값일 뿐.

 

.on() method 에 대해 알아보고 싶다면, 여기를 클릭.

 

How is JavaScript .on() method defined?

In the jQuery library, the function doesn't exist, yet every jQuery object has these essential methods. In another thread, it was stated that .on() belongs to the node API, which confuses me, since...

stackoverflow.com

요약하자면,

"on method registers a handler, which is callback function with specific signature. Once an event is triggered, a handler is called. It receives necessary data as function parameters (commonly event object)."

이다.

 

참조: https://helicopter55.tistory.com/52