► JS Algorithm/Programmers

[Programmers] JavaScript 알고리즘 | Lv.0 덧셈식 출력하기

다람트리 2024. 7. 7. 22:33
반응형

🔒 문제 설명

두 정수 a, b가 주어질 때 다음과 같은 형태의 계산식을 출력하는 코드를 작성해 보세요.

a + b = c


🔒 제한사항 

  • 1 ≤ a, b ≤ 100

🔒 입출력 예

입력 #1

4 5

출력 #1

4 + 5 = 9

 

🔐 solution of mine

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

let input = [];

rl.on('line', function (line) {
    input = line.split(' ');
}).on('close', function () {
    console.log(`${Number(input[0])} + ${Number(input[1])} = ${Number(input[0])+Number(input[1])}`);
});

 


 

🔐 solution of others 

String.split()

const readline = require('readline')
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
}).on('line', function (line) {
    const [a, b] = line.split(' ')
    console.log(a, '+', b, '=', Number(a) + Number(b))
})

split() 메서드는 String 객체를 지정한 구분자를 이용하여 여러 개의 문자열로 나눕니다.

 

🔐 solution of others 

String.split()

Array.map(Number)

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

let a, b;

rl.on('line', function (line) {
    [a, b] = line.split(' ').map(Number);
}).on('close', function () {
    console.log(`${a} + ${b} = ${a + b}`);
});

map() 메서드는 배열 내의 모든 요소 각각에 대하여 주어진 함수를 호출한 결과를 모아 새로운 배열을 반환합니다.

 

🔐 solution of others 

String.split()

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

let input = [];

rl.on('line', function (line) {
    input = line.split(' ');
}).on('close', function () {
    const a = Number(input[0]);
    const b = Number(input[1]);
    console.log(`${a} + ${b} = ${a+b}`)
});

 


 

반응형