Jieunny์˜ ๋ธ”๋กœ๊ทธ

S2) Unit 1. [JS] ๊ณ ์ฐจํ•จ์ˆ˜ ๋ณธ๋ฌธ

CodeStates/learning contents

S2) Unit 1. [JS] ๊ณ ์ฐจํ•จ์ˆ˜

Jieunny 2023. 1. 12. 10:12

๐Ÿ“ฃ ์ผ๊ธ‰ ๊ฐ์ฒด

โœ”๏ธ JS์—์„œ ํŠน๋ณ„ํ•˜๊ฒŒ ์ทจ๊ธ‰๋˜๋Š” ๊ฐ์ฒด <ํ•จ์ˆ˜>

โžฐ ๋ณ€์ˆ˜์— ํ• ๋‹น ํ•  ์ˆ˜ ์žˆ๋‹ค.
โžฐ ๋‹ค๋ฅธ ํ•จ์ˆ˜์˜ ์ „๋‹ฌ์ธ์ž๋กœ ์ „๋‹ฌ๋  ์ˆ˜ ์žˆ๋‹ค.
โžฐ ๋‹ค๋ฅธ ํ•จ์ˆ˜์˜ ๊ฒฐ๊ณผ๋กœ์„œ ๋ฆฌํ„ด๋  ์ˆ˜ ์žˆ๋‹ค.

 

๐Ÿ“ฃ ๊ณ ์ฐจ ํ•จ์ˆ˜

โœ”๏ธ ํ•จ์ˆ˜๋ฅผ ์ „๋‹ฌ์ธ์ž๋กœ ๋ฐ›์„ ์ˆ˜ ์žˆ๊ณ , ํ•จ์ˆ˜๋ฅผ ๋ฆฌํ„ดํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜

โžฐ  ๋‹ค๋ฅธ ํ•จ์ˆ˜์˜ ์ „๋‹ฌ์ธ์ž๋กœ ์ „๋‹ฌ๋˜๋Š” ํ•จ์ˆ˜ <์ฝœ๋ฐฑ ํ•จ์ˆ˜>
โžฐ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ์ „๋‹ฌ ๋ฐ›์€ ๊ณ ์ฐจ ํ•จ์ˆ˜๋Š” ๋‚ด๋ถ€์—์„œ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœ ํ•  ์ˆ˜ ์žˆ๊ณ , ์กฐ๊ฑด์— ๋”ฐ๋ผ ์‹คํ–‰ ์—ฌ๋ถ€๋ฅผ ๊ฒฐ์ •ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.
โžฐ ํ•จ์ˆ˜๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜ <์ปค๋ง ํ•จ์ˆ˜> (๋”ฐ๋กœ ์ปค๋งํ•จ์ˆ˜ ๋ผ๋Š” ์šฉ์–ด๋ฅผ ์“ธ ํ•„์š”๋Š” ์—†๋‹ค)
โžฐ ์ปค๋งํ•จ์ˆ˜ ⊂ ๊ณ ์ฐจ ํ•จ์ˆ˜ 

 

๐Ÿ“ฃ ๋‚ด์žฅ ๊ณ ์ฐจ ํ•จ์ˆ˜

1๏ธโƒฃ ๋ฐฐ์—ด ๋ฉ”์„œ๋“œ filter(๊ฑธ๋Ÿฌ๋‚ด๋Š” ๊ธฐ์ค€)

๋ชจ๋“  ๋ฐฐ์—ด์˜ ์š”์†Œ ์ค‘์—์„œ ํŠน์ • ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์š”์†Œ๋ฅผ ๊ฑธ๋Ÿฌ๋‚ด๋Š” ๋ฉ”์„œ๋“œ
๊ฑธ๋Ÿฌ๋‚ด๊ธฐ ์œ„ํ•œ ์กฐ๊ฑด์„ ๋ช…์‹œํ•œ ํ•จ์ˆ˜๋ฅผ ์ „๋‹ฌ์ธ์ž๋กœ ๋ฐ›๋Š”๋‹ค.
๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ์ธ์ž๋กœ ์ „๋‹ฌ๋ฐ›์€ ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•œ๋‹ค.
์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋ฉด true, ์•„๋‹ˆ๋ฉด false๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
true๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ์š”์†Œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์‹ ๊ทœ ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด ๋ฐ˜ํ™˜ํ•œ๋‹ค.
let arr = [1, 2, 3];
// ๋ฐฐ์—ด์˜ filter ๋ฉ”์„œ๋“œ๋Š” ํ•จ์ˆ˜๋ฅผ ์ „๋‹ฌ์ธ์ž๋กœ ๋ฐ›๋Š” ๊ณ ์ฐจ ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
// arr.filter๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ๋‚ด๋ถ€์ ์œผ๋กœ arr์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•ด๋„ ๋ฉ๋‹ˆ๋‹ค.
arr.filter = function (arr, func) {
  const newArr = [];
  for (let i = 0; i < arr.length; i++) {
    // filter์— ์ „๋‹ฌ์ธ์ž๋กœ ์ „๋‹ฌ๋œ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋Š” arr์˜ ๊ฐ ์š”์†Œ๋ฅผ ์ „๋‹ฌ๋ฐ›์•„ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.
    // ์ฝœ๋ฐฑ ํ•จ์ˆ˜๊ฐ€ true๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ๊ฒฝ์šฐ์—๋งŒ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์— ์ถ”๊ฐ€๋ฉ๋‹ˆ๋‹ค.
    if (func(arr[i]) === true) {
      newArr.push(this[i]);
    }
  }
  // ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ๊ฒฐ๊ณผ๊ฐ€ true์ธ ์š”์†Œ๋“ค๋งŒ ์ €์žฅ๋œ ๋ฐฐ์—ด์„ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.
  return newArr;
};
// ํ•จ์ˆ˜ ํ‘œํ˜„์‹
const isEven = function (num) {
  return num % 2 === 0;
};

let arr = [1, 2, 3, 4];
// let output = arr.filter(์ง์ˆ˜);
// '์ง์ˆ˜'๋ฅผ ํŒ๋ณ„ํ•˜๋Š” ํ•จ์ˆ˜๊ฐ€ ์กฐ๊ฑด์œผ๋กœ์„œ filter ๋ฉ”์„œ๋“œ์˜ ์ „๋‹ฌ์ธ์ž๋กœ ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค.
let output = arr.filter(isEven);	// isEven์ด๋ผ๋Š” ๋ฉ”์„œ๋“œ๊ฐ€ ์ „๋‹ฌ๋œ๋‹ค.
console.log(output); // ->> [2, 4]

const isLteFive = function (str) {
  // Lte = less then equal
  return str.length <= 5;
};

arr = ['hello', 'code', 'states', 'happy', 'hacking'];
// output = arr.filter(๊ธธ์ด 5 ์ดํ•˜)
// '๊ธธ์ด 5 ์ดํ•˜'๋ฅผ ํŒ๋ณ„ํ•˜๋Š” ํ•จ์ˆ˜๊ฐ€ ์กฐ๊ฑด์œผ๋กœ์„œ filter ๋ฉ”์„œ๋“œ์˜ ์ „๋‹ฌ์ธ์ž๋กœ ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค.
let output = arr.filter(isLteFive);
console.log(output); // ->> ['hello', 'code', 'happy']

 

๐Ÿ“Œ ๋ฌธ์ œ

๋งŒํ™”์ฑ… ์‹๊ฐ 27๊ถŒ์˜ ์ •๋ณด๊ฐ€ ๋ฐฐ์—ด์— ๋‹ด๊ฒจ์žˆ์Šต๋‹ˆ๋‹ค. ์ถœํŒ ์—ฐ๋„๊ฐ€ 2003๋…„์ธ ๋‹จํ–‰๋ณธ๋งŒ ๋‹ด์€ ๋ฐฐ์—ด์„ ๋งŒ๋“œ์„ธ์š”.

 

โœ๏ธ ํ’€์ด

// ๋‹จํ–‰๋ณธ ๋ชจ์Œ
const cartoons = [
  {
    id: 1,
    bookType: 'cartoon',
    title: '์‹๊ฐ',
    subtitle: '์–ด๋จธ๋‹ˆ์˜ ์Œ€',
    createdAt: '2003-09-09',
    genre: '์š”๋ฆฌ',
    artist: 'ํ—ˆ์˜๋งŒ',
    averageScore: 9.66,
  },
  {
    id: 2,
    // .. ์ดํ•˜ ์ƒ๋žต
  },
  // ... ์ดํ•˜ ์ƒ๋žต
]; 

// ๋‹จํ–‰๋ณธ ํ•œ ๊ถŒ์˜ ์ถœํŒ ์—ฐ๋„๊ฐ€ 2003์ธ์ง€ ํ™•์ธํ•˜๋Š” ํ•จ์ˆ˜
const isCreatedAt2003 = function (cartoon) {
  const fullYear = new Date(cartoon.createdAt).getFullYear()
  return fullYear === 2003;
}; 

// ์ถœํŒ ์—ฐ๋„๊ฐ€ 2003๋…„์ธ ์ฑ…์˜ ๋ชจ์Œ
const filteredCartoons = cartoons.filter(isCreatedAt2003);

 

2๏ธโƒฃ map

๋ชจ๋“  ๋ฐฐ์—ด์˜ ๊ฐ’์— ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๋ฉ”์„œ๋“œ์ด๋‹ค.
์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•œ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์ง€๊ณ  ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋งŒ๋“ ๋‹ค.
์ฝœ๋ฐฑํ•จ์ˆ˜์— ๋‘ ๋ฒˆ์งธ ์ „๋‹ฌ์ธ์ž๋ฅผ ์ค„ ๊ฒฝ์šฐ ์ธ๋ฑ์Šค๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.

 

๐Ÿ“Œ ๋ฌธ์ œ

๋งŒํ™”์ฑ… ์‹๊ฐ 27๊ถŒ์˜ ์ •๋ณด๊ฐ€ ๋ฐฐ์—ด์— ๋‹ด๊ฒจ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ์ฑ…์˜ ๋ถ€์ œ(subtitle)๋งŒ ๋‹ด์€ ๋ฐฐ์—ด์„ ๋งŒ๋“œ์„ธ์š”.

 

โœ๏ธ ํ’€์ด

// ๋งŒํ™”์ฑ… ๋ชจ์Œ
const cartoons = [
  {
    id: 1,
    bookType: 'cartoon',
    title: '์‹๊ฐ',
    subtitle: '์–ด๋จธ๋‹ˆ์˜ ์Œ€',
    createdAt: '2003-09-09',
    genre: '์š”๋ฆฌ',
    artist: 'ํ—ˆ์˜๋งŒ',
    averageScore: 9.66,
  },
  {
    id: 2,
    // .. ์ดํ•˜ ์ƒ๋žต
  },
  // ... ์ดํ•˜ ์ƒ๋žต
]; 

// ๋งŒํ™”์ฑ… ํ•œ ๊ถŒ์˜ ๋ถ€์ œ๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ๋กœ์ง(ํ•จ์ˆ˜)
const findSubtitle = function (cartoon) {
  return cartoon.subtitle;
}; 

// ๊ฐ ์ฑ…์˜ ๋ถ€์ œ ๋ชจ์Œ 
const subtitles = cartoons.map(findSubtitle); // ['์–ด๋จธ๋‹ˆ์˜ ์Œ€', ...]

 

 

3๏ธโƒฃ reduce

๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ ์ฝœ๋ฐฑํ•จ์ˆ˜์˜ ์‹คํ–‰ ๊ฐ’์„ ๋ˆ„์ ํ•˜์—ฌ ํ•˜๋‚˜์˜ ๊ฒฐ๊ณผ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
๋ฐฐ์—ด์ด ๋“ค์–ด๊ฐ€์„œ ํ•˜๋‚˜์˜ ๊ฐ’์ด ๋‚˜์˜จ๋‹ค.
์ฒซ ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” '์ดˆ๊ธฐ๊ฐ’' ๋˜๋Š” '๋ˆ„์ ๊ฐ’'
๋‘ ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” 'ํ˜„์žฌ๊ฐ’'

 

๐Ÿ“Œ ๋ฌธ์ œ

๋งŒํ™”์ฑ… ์‹๊ฐ 27๊ถŒ์˜ ์ •๋ณด๊ฐ€ ๋ฐฐ์—ด์— ๋‹ด๊ฒจ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ๋‹จํ–‰๋ณธ์˜ ํ‰์ ์˜ ํ‰๊ท ์„ ๋ฆฌํ„ดํ•˜์„ธ์š”.

 

โœ๏ธ ํ’€์ด

// ๋‹จํ–‰๋ณธ ๋ชจ์Œ
const cartoons = [
  {
    id: 1,
    bookType: 'cartoon',
    title: '์‹๊ฐ',
    subtitle: '์–ด๋จธ๋‹ˆ์˜ ์Œ€',
    createdAt: '2003-09-09',
    genre: '์š”๋ฆฌ',
    artist: 'ํ—ˆ์˜๋งŒ',
    averageScore: 9.66,
  },
  {
    id: 2,
    // .. ์ดํ•˜ ์ƒ๋žต
  },
  // ... ์ดํ•˜ ์ƒ๋žต
];

// ๋‹จํ–‰๋ณธ ํ•œ ๊ถŒ์˜ ํ‰์ ์„ ๋ˆ„์ ๊ฐ’์— ๋”ํ•œ๋‹ค.
const scoreReducer = function (sum, cartoon) {
  return sum + cartoon.averageScore;
}; 

// ์ดˆ๊ธฐ๊ฐ’์— 0์„ ์ฃผ๊ณ , ์ˆซ์ž์˜ ํ˜•ํƒœ๋กœ ํ‰์ ์„ ๋ˆ„์ ํ•œ๋‹ค.
let initialValue = 0 
// ๋ชจ๋“  ์ฑ…์˜ ํ‰์ ์„ ๋ˆ„์ ํ•œ ํ‰๊ท ์„ ๊ตฌํ•œ๋‹ค.
const cartoonsAvgScore = cartoons.reduce(scoreReducer, initialValue) / cartoons.length;

 

โžฐ ๋ฐฐ์—ด -> ๋ฌธ์ž์—ด, ๋ฐฐ์—ด -> ๊ฐ์ฒด ์— ๋งŽ์ด ์‚ฌ์šฉ๋œ๋‹ค.

 

๐Ÿ“Œ ๋ฌธ์ œ

๋ฐฐ์—ด์„ ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊ฟ”๋ผ.

 

โœ๏ธ ํ’€์ด

// ์ฝœ๋ฐฑ ํ•จ์ˆ˜ joinName์€ users ๋ฐฐ์—ด ์•ˆ์— ์žˆ๋Š” ์š”์†Œ์˜ ์ด๋ฆ„์„ ํ•˜๋‚˜๋กœ ์‘์ถ•ํ•œ๋‹ค.
function joinName(resultStr, user) {
  resultStr = resultStr + user.name + ', ';
  return resultStr;
}

let users = [
  { name: 'Tim', age: 40 },
  { name: 'Satya', age: 30 },
  { name: 'Sundar', age: 50 }
];

users.reduce(joinName, '');
// 'Tim, Satya, Sundar, '

 

 

๐Ÿ“Œ ๋ฌธ์ œ

๋ฐฐ์—ด์„ ๊ฐ์ฒด๋กœ ๋ฐ”๊ฟ”๋ผ.

 

โœ๏ธ ํ’€์ด

// ์ฝœ๋ฐฑ ํ•จ์ˆ˜ makeAddressBook์€ users ๋ฐฐ์—ด ์•ˆ์— ์žˆ๋Š” ์š”์†Œ๋กœ ์ฃผ์†Œ๋ก์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค.
function makeAddressBook(addressBook, user) {
  let firstLetter = user.name[0];

  if(firstLetter in addressBook) {
    addressBook[firstLetter].push(user);	// ๋งจ ์•ž๊ธ€์ž๊ฐ€ ์ด๋ฏธ ๊ฐ์ฒด์— ์žˆ๋Š” ๊ฒฝ์šฐ
  } else {
    addressBook[firstLetter] = [];
    addressBook[firstLetter].push(user);
  }

  return addressBook;
}

let users = [
  { name: 'Tim', age: 40 },
  { name: 'Satya', age: 30 },
  { name: 'Sundar', age: 50 }
];

users.reduce(makeAddressBook, {});
// {
//  T: [
//    { name: 'Tim', age: 40 }
//  ],
//  S: [
//    { name: 'Satya', age: 30 },
//    { name: 'Sundar', age: 50 }
//  ]
//}

 

 

๐Ÿ“ฃ ๊ณ ์ฐจ ํ•จ์ˆ˜๋ฅผ ์“ฐ๋Š” ์ด์œ 

โœ”๏ธ ์ถ”์ƒํ™” 

๋ณต์žกํ•œ ์–ด๋–ค ๊ฒƒ์„ ์••์ถ•ํ•ด์„œ ํ•ต์‹ฌ๋งŒ ์ถ”์ถœํ•œ ์ƒํƒœ๋กœ ๋งŒ๋“œ๋Š” ๊ฒƒ
์ถ”์ƒํ™”๋ฅผ ์ด์šฉํ•˜๋ฉด ํšจ์œจ์ ์ด๊ณ  ํŽธํ•˜๊ฒŒ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๋‹ค.
์ƒ์‚ฐ์„ฑ์˜ ํ–ฅ์ƒ 

 

โœ”๏ธ ์ถ”์ƒํ™”์˜ ์ข‹์€ ์‚ฌ๋ก€์ธ ํ•จ์ˆ˜ -> ํ•จ์ˆ˜๋Š” ์‚ฌ๊ณ  ๋˜๋Š” ๋…ผ๋ฆฌ์˜ ๋ฌถ์Œ

ํ•จ์ˆ˜ = ๊ฐ’์„ ์ „๋‹ฌ๋ฐ›์•„ ๊ฐ’์„ ๋ฆฌํ„ดํ•œ๋‹ค = ๊ฐ’์— ๋Œ€ํ•œ ๋ณต์žกํ•œ ๋กœ์ง์€ ๊ฐ์ถ”์–ด์ ธ ์žˆ๋‹ค = ๊ฐ’ ์ˆ˜์ค€์—์„œ์˜ ์ถ”์ƒํ™”
๊ณ ์ฐจ ํ•จ์ˆ˜ = ํ•จ์ˆ˜๋ฅผ ์ „๋‹ฌ๋ฐ›๊ฑฐ๋‚˜ ํ•จ์ˆ˜๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค = ์‚ฌ๊ณ (ํ•จ์ˆ˜)์— ๋Œ€ํ•œ ๋ณต์žกํ•œ ๋กœ์ง์€ ๊ฐ์ถ”์–ด์ ธ ์žˆ๋‹ค = ์‚ฌ๊ณ  ์ˆ˜์ค€์—์„œ์˜ ์ถ”์ƒํ™”

 

๐Ÿ“Œ ๋ฌธ์ œ

์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐ์˜ ๋‚จ์„ฑ๋“ค์˜ ํ‰๊ท  ๋‚˜์ด๋ฅผ ๊ตฌํ•ด๋ผ.

// ์˜ˆ์‹œ ๋ฐ์ดํ„ฐ
const data = [
  {
    gender: 'male',
    age: 24,
  },
  {
    gender: 'male',
    age: 25,
  },
  {
    gender: 'female',
    age: 27,
  },
  {
    gender: 'female',
    age: 22,
  },
  {
    gender: 'male',
    age: 29,
  },
];
function getAverageAgeOfMaleAtOnce(data) {
  const onlyMales = data.filter(function (d) {
    // data.filter๋Š” ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ์ธ์ž๋กœ ์ „๋‹ฌ๋ฐ›์€ ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•˜๊ณ ,
    // ๊ทธ ๊ฒฐ๊ณผ๊ฐ€ true์ธ ์š”์†Œ๋งŒ์„ ๊ฐ–๋Š” ๋ฐฐ์—ด์„ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.
    return d.gender === 'male';
  });

  const numOfMales = onlyMales.length;

  const onlyMaleAges = onlyMales.map(function (d) {
    // onlyMales.map๋Š” ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ์ธ์ž๋กœ ์ „๋‹ฌ๋ฐ›์€ ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•˜๊ณ ,
    // ๊ฐ ๊ฒฐ๊ณผ๋ฅผ ์š”์†Œ๋กœ ๊ฐ–๋Š” ๋ฐฐ์—ด์„ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.
    return d.age;
  });

  const sumOfAges = onlyMaleAges.reduce(function (acc, cur) {
    // onlyMaleAges.reduce๋Š” ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ์ธ์ž๋กœ ์ „๋‹ฌ๋ฐ›์€ ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•˜๊ณ ,
    // ๊ฐ ๊ฒฐ๊ณผ๋ฅผ ๋‘ ๋ฒˆ์งธ ์ธ์ž๋กœ ์ „๋‹ฌ๋ฐ›์€ ์ดˆ๊ธฐ๊ฐ’(0)์— ๋ˆ„์ ํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.
    return acc + cur;
  }, 0);

  return sumOfAges / numOfMales;
}

 

function getOnlyMales(data) {
  return data.filter(function (d) {
    return d.gender === 'male';
  });
}

function getOnlyAges(data) {
  return data.map(function (d) {
    return d.age;
  });
}

function getAverage(data) {
  const sum = data.reduce(function (acc, cur) {
    return acc + cur;
  }, 0);
  return sum / data.length;
}

function compose(...funcArgs) {	// ์ž„๋ ฅ๋œ ํ•จ์ˆ˜๋ฅผ ์ˆœ์ฐจ์ ์œผ๋กœ ์‹คํ–‰ํ•˜๋Š” ๊ณ ์ฐจ ํ•จ์ˆ˜ compose
  // compose๋Š” ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ•จ์ˆ˜๋ฅผ ์ธ์ž๋กœ ์ „๋‹ฌ๋ฐ›์•„ ํ•จ์ˆ˜๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ๊ณ ์ฐจ ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
  // compose๊ฐ€ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜(์ต๋ช… ํ•จ์ˆ˜)๋Š” ์ž„์˜์˜ ํƒ€์ž…์˜ data๋ฅผ ์ž…๋ ฅ๋ฐ›์•„,
  return function (data) {
    // funcArgs์˜ ์š”์†Œ์ธ ํ•จ์ˆ˜๋“ค์„ ์ฐจ๋ก€๋Œ€๋กœ ์ ์šฉ(apply)์‹œํ‚จ ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.
    let result = data;
    for (let i = 0; i < funcArgs.length; i++) {	// ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋“ค์–ด์˜จ ํ•จ์ˆ˜์˜ ๊ฐœ์ˆ˜๋งŒํผ ๋ˆ๋‹ค.
      result = funcArgs[i](result);
    }
    return result;
  };
}

// compose๋ฅผ ํ†ตํ•ด ํ•จ์ˆ˜๋“ค์ด ์ˆœ์„œ๋Œ€๋กœ ์ ์šฉ๋œ๋‹ค๋Š” ๊ฒƒ์ด ์ง๊ด€์ ์œผ๋กœ ๋“œ๋Ÿฌ๋‚ฉ๋‹ˆ๋‹ค.
// ๊ฐ๊ฐ์˜ ํ•จ์ˆ˜๋Š” ๋‹ค๋ฅธ ๋ชฉ์ ์„ ์œ„ํ•ด ์žฌ์‚ฌ์šฉ(reuse) ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
const getAverageAgeOfMale = compose(
  getOnlyMales, // ๋ฐฐ์—ด์„ ์ž…๋ ฅ๋ฐ›์•„ ๋ฐฐ์—ด์„ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜
  getOnlyAges, // ๋ฐฐ์—ด์„ ์ž…๋ ฅ๋ฐ›์•„ ๋ฐฐ์—ด์„ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜
  getAverage // ๋ฐฐ์—ด์„ ์ž…๋ ฅ๋ฐ›์•„ `number` ํƒ€์ž…์„ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜
);

const result = getAverageAgeOfMale(data); (์ง€๊ธˆ ๋ฐ์ดํ„ฐ๋Š” ์ฃผ์–ด์ง€์ง€ ์•Š์•˜์Œ)
console.log(result); // --> 26

 

๐Ÿšจ ๊ณ ์ฐจ ํ•จ์ˆ˜๊ฐ€ ์•„๋‹Œ ๊ฒƒ

ํ•จ์ˆ˜๋ฅผ ์ „๋‹ฌ์ธ์ž๋กœ ๋ฐ›์ง€ ์•Š๊ณ , ํ•จ์ˆ˜๋ฅผ ๋ฆฌํ„ดํ•˜๊ณ  ์žˆ์ง€ ์•Š๋‹ค.
function calcuate(x, y, z){
	const plus = function (num1, num2){
		return num1 + num2;
	}
	return plus(x, y) * z
}