강의

멘토링

커뮤니티

Inflearn Community Q&A

kal65296221's profile image
kal65296221

asked

Introduction to Javascript Algorithm Problem Solving (Coding Test Preparation)

7. 10 subtitles

리뷰 부탁드립니다!

Written on

·

481

0

이런 식의 풀이는 조금 더 안좋은 풀이 방식일까요?

 

function solution(n, arr){
  const answer = arr.filter((item) => String(item).charAt(1) === String(n))
  return answer.length;
}

const arr=[12, 20, 54, 30, 87, 91, 30];
console.log(solution(0, arr));
javascript코딩-테스트코테 준비 같이 해요!

Answer 1

0

숫자를 비교해서 해결할 수 있는 문제를 굳이 문자열로 변환한 다음 인덱스 값에 접근해 가면서까지 하면 비효율적이지 않을까요?

저도 강사님과 같은 개념으로 접근했는데 질문자님처럼 filter를 사용해서 코드가 깔끔하게 나왔습니다

function solution(date, ...numbers) { return [...numbers].filter((a) => a % 10 === date).length; }

kal65296221님의 프로필 이미지
kal65296221
Questioner

맞네요! 정근님 코드에서 굳이 스프레드 연산자 없이도 가능하네요!

function solution(n, arr){
  return arr.filter((item) => item % 10 === n).length
}

const arr=[12, 20, 54, 30, 87, 91, 30];

console.log(solution(0, arr));
kal65296221's profile image
kal65296221

asked

Ask a question