๐Ÿค ์€์ง€log ๐Ÿค

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค:Javascript] ์ค‘๋ณต๋œ ์ˆซ์ž ๊ฐœ์ˆ˜ ๋ณธ๋ฌธ

๐ŸŒˆ ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค:Javascript] ์ค‘๋ณต๋œ ์ˆซ์ž ๊ฐœ์ˆ˜

Eun_zii 2023. 10. 13. 14:12

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค Lv.0

- ์ค‘๋ณต๋œ ์ˆซ์ž ๊ฐœ์ˆ˜

 

๐Ÿ’š ๋ฌธ์ œ์„ค๋ช…

  • ์ •์ˆ˜๊ฐ€ ๋‹ด๊ธด ๋ฐฐ์—ด array์™€ ์ •์ˆ˜ n์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, array์— n์ด ๋ช‡ ๊ฐœ ์žˆ๋Š” ์ง€๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด๋ณด์„ธ์š”.

 

๐Ÿ’š ์ œํ•œ์กฐ๊ฑด

  • 1 ≤ array์˜ ๊ธธ์ด ≤ 100
  • 0 ≤ array์˜ ์›์†Œ ≤ 1,000
  • 0 ≤ n ≤ 1,000

 

๐Ÿ’š ๋ฌธ์ œ

function solution(array, n) {
    var answer = 0;
    return answer;
}

 

 

๐Ÿ’š ํ’€์ด

function solution(array, n) {
  return array.filter(x => x === n).length;
}

 

 

๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ x๊ฐ€ ์ œ๊ณต๋œ ์ˆซ์ž n๊ณผ ๊ฐ™์€์ง€ ํ™•์ธํ•œํ›„, ์š”์†Œ๊ฐ€ n๊ณผ ๊ฐ™์œผ๋ฉด true๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ  ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด false๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

๊ทธํ›„ ํ•„ํ„ฐ๋ง๋œ ๊ฒฐ๊ณผ ๋ฐฐ์—ด์˜ ๊ธธ์ด๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฐœ์ˆ˜๋Š” 'n' ๊ฐ’์ด ์›๋ž˜ ๋ฐฐ์—ด์— ๋‚˜ํƒ€๋‚˜๋Š” ํšŸ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.

 

 

โœ…  filter() 

: filter() ๋ฉ”์„œ๋“œ๋Š” ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์˜ ์ผ๋ถ€์— ๋Œ€ํ•œ ์–•์€ ๋ณต์‚ฌ๋ณธ์„ ์ƒ์„ฑํ•˜๊ณ , ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์—์„œ ์ œ๊ณต๋œ ํ•จ์ˆ˜์— ์˜ํ•ด ๊ตฌํ˜„๋œ ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•œ ์š”์†Œ๋กœ๋งŒ ํ•„ํ„ฐ๋ง ํ•ฉ๋‹ˆ๋‹ค.

const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];

const result = words.filter((word) => word.length > 6);

console.log(result);
// Expected output: Array ["exuberant", "destruction", "present"]

 

728x90