-
자바스크립트 값 입력 받기프로그래밍 언어 😵💫/JavaScript 2023. 3. 14. 01:55
코테에서 값 입력 받는 방법
1. node.js의 file system 모듈 불러오기
const fs = require('fs');2. fs 모듈의 readFileSync함수를 통해 해당경로 파일 전체 읽기
- '/dev/stdin' 경로에 테스트 파일 있음(백준)
- 읽은 정보 toString()함수로 문자열 변환 사용
- 문자열을 배열화 split(), 공백 기준 문자 나누어 값 저장 split(' ')나 split('\n')처럼
ex) 2 3 입력하면 input data = ['2', '3']
const inputData1 = fs.readFileSync('/dev/stdin').toString().split(' ');3. 숫자의 경우
.map(value => +value)
const inputDataNum = fs.readFileSync('/dev/stdin').toString().splite(' ').map(value => +value);4. 여러줄로 입력받을 때
const inputDataStr = fs.readFileSync('/dev/stdin').toString().split('\n');문제 두 정수 A와 B 입력받아 A+B 출력하기
const fs = require('fs'); const inputData = fs.readFileSync('/dev/stdin').toString().split(' ').map(value => +value); const [a, b] = inputData; consloe.log(A+B);'프로그래밍 언어 😵💫 > JavaScript' 카테고리의 다른 글
[프로그래머스] 점의 위치 구하기, 크기가 작은 부분문자열 (2) 2023.03.29 [프로그래머스] 삼총사 자바스크립트 (0) 2023.03.27 javascript 2 (1) 2023.03.06 javascript 1 (2) 2023.03.05