프로그래머스[Level 2] 오픈채팅방

bkboy·2022년 6월 28일
0

문제

링크

제한 사항

입출력 예

풀이

function solution(record) {
  const action = [];
  const obj = {};

  for (const str of record) {
    const tmp = str.split(" ");
    if (tmp[2]) {
      obj[tmp[1]] = tmp[2];
    }
    if (tmp[0] === "Enter" || tmp[0] === "Leave") {
      action.push(tmp);
    }

    if (tmp[0] === "Chage") {
      obj[tmp[1]] = tmp[2];
    }
  }

  const answer = [];
  for (let [command, userId] of action) {
    if (command === "Enter") {
      answer.push(`${obj[userId]}님이 들어왔습니다.`);
    } else {
      answer.push(`${obj[userId]}님이 나갔습니다.`);
    }
  }
  return answer;
}

간단한 구현 문제이다.

입력받은 문자열을 split으로 나눈다.
객체에 아이디에 따른 닉네임 즉, 데이터를 저장해둔다.

행동과 관련된 command(enter, leave)는 다른 배열에 따로 저장해둔 뒤 나중에 구현해준다.

profile
음악하는 개발자

0개의 댓글