본문 바로가기
Javascript

오답노트) 구조분해할당과 filter사용방법

by jennyiscoding 2022. 9. 29.

리펙토링 전

const readline = require('readline');

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});
let input = [];
rl.on('line', function (x) {
  input.push(x);
  if (input.length >= 3) {
    rl.close();
  }
}).on('close', function () {
  letcardssplit = input[1].split(' ');
  answer = 0;
  letcardssplit.map(v => {
    if (v === input[2]) {
      answer++;
    }
  });
  console.log(answer);
  process.exit();
});

바꾼거

const readline = require('readline');

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});
let input = [];
rl.on('line', function (x) {
  input.push(x);
  if (input.length >= 3) {
    rl.close();
  }
}).on('close', function () {
  const [first, second, third] = input;
  const secondArr = second.split(' ');
  let count = secondArr.filter(el => el == third).length;

  console.log(count);
  process.exit();
});

알게된것 1.구조분해할당

const [first, second, third] = input;

알게된것 2. filter쓰는 방법

let count = secondArr.filter(el => el == third).length;