본문 바로가기
프론트엔드로 가는 길/프로그래머스

Hash : 의상

by woody-j 2023. 11. 22.

https://school.programmers.co.kr/learn/courses/30/lessons/42578

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

  function solution(clothes) {
    //     1. 초기값 설정
    let clothesObj = {};
    let result = 1;
    for (let i = 0; i < clothes.length; i++) {
      //     2. 객체 할당
      let [itemName, category] = clothes[i];
      //         3. 카테고리가 있으면 1 증가, 없으면 1 할당
      clothesObj[category] = (clothesObj[category] || 0) + 1;
    }
    for (const property in clothesObj) {
      //     4. 카운트 값 +1 후 곱하고 -1
      result *= clothesObj[property] + 1;
    }
    return result - 1;
  }

 

  1. clothesObj[category] = (clothesObj[category] || 0) + 1;

 clothesObj[category] 가 있으면 +1 없으면 0 으로 

 

  2. for (const property in clothesObj) {

clothessObj의 value으로 계산

 

Object.values 로도 value을 가지고 올 수 있다.

-> Object.values(clothesObj).reduce((acc, val) => acc * (val + 1), 1);