Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

13 장 메서드 체이닝 연습문제를 풀어봅시다 #10

Open
sa02045 opened this issue Jul 15, 2023 · 0 comments
Open

13 장 메서드 체이닝 연습문제를 풀어봅시다 #10

sa02045 opened this issue Jul 15, 2023 · 0 comments
Assignees
Labels

Comments

@sa02045
Copy link

sa02045 commented Jul 15, 2023

문제 1

배열에서 투자금이 5000 이상인 투자자들을 찾고, 나이대별로 그룹화하여 이름과 수익을 보여주세요

// investment 투자금 
// returnRate 수익률 
// 수익: 투자금 * 수익률

const investors = [
    { name: "John", age: 35, investment: 5000, returnRate: 0.15 },
    { name: "Alice", age: 42, investment: 8000, returnRate: 0.12 },
    { name: "Bob", age: 28, investment: 3000, returnRate: 0.18 },
    { name: "Carol", age: 55, investment: 10000, returnRate: 0.13 },
    { name: "David", age: 47, investment: 6000, returnRate: 0.11 },
    { name: "Eve", age: 31, investment: 7000, returnRate: 0.16 },
    { name: "Frank", age: 39, investment: 9000, returnRate: 0.19 },
    { name: "Grace", age: 44, investment: 12000, returnRate: 0.14 },
    { name: "Henry", age: 52, investment: 1000, returnRate: 0.17 },
    { name: "Ivy", age: 36, investment: 4000, returnRate: 0.10 },
    { name: "Jane", age: 28, investment: 2000, returnRate: 0.15 },
    { name: "Kate", age: 29, investment: 3000, returnRate: 0.12 },
    { name: "Luke", age: 26, investment: 4000, returnRate: 0.18 },
    { name: "Mark", age: 27, investment: 2500, returnRate: 0.13 },
    { name: "Nancy", age: 25, investment: 1500, returnRate: 0.11 }
  ];

// 나이대 별 그룹화 함수
function getAgeRange(age) {
  if (age >= 20 && age < 30) {
    return "20s";
  } else if (age >= 30 && age < 40) {
    return "30s";
  } else if (age >= 40 && age < 50) {
    return "40s";
  } else if (age >= 50 && age < 60) {
    return "50s";
  } else {
    return "Unknown";
  }
}

조건

  • 배열 메서드 map, filter, reduce를 사용해서 체이닝 메서드를 활용
  • 화살표 함수 사용하기 or 콜백에 이름 붙이기

원하는 출력값

{
  '30s': [
    {
      name: 'John',
      age: 35,
      investment: 5000,
      returnRate: 0.15,
      profit: 750
    },
    {
      name: 'Eve',
      age: 31,
      investment: 7000,
      returnRate: 0.16,
      profit: 1120
    },
    {
      name: 'Frank',
      age: 39,
      investment: 9000,
      returnRate: 0.19,
      profit: 1710
    }
  ],
  '40s': [
    {
      name: 'Alice',
      age: 42,
      investment: 8000,
      returnRate: 0.12,
      profit: 960
    },
    {
      name: 'David',
      age: 47,
      investment: 6000,
      returnRate: 0.11,
      profit: 660
    },
    {
      name: 'Grace',
      age: 44,
      investment: 12000,
      returnRate: 0.14,
      profit: 1680.0000000000002
    }
  ],
  '50s': [
    {
      name: 'Carol',
      age: 55,
      investment: 10000,
      returnRate: 0.13,
      profit: 1300
    }
  ]
}

문제2

주문 기록을 토대로 총 주문의 총 금액을 계산하여 반환하세요.

// orders : 주문 배열
// products: 상품 배열
// productId: 상품 번호
// price: 상품 금액
// 주문 금액: quantity * price

const orders = [
  { id: 1, productId: 1, quantity: 2 },
  { id: 2, productId: 2, quantity: 1 },
  { id: 3, productId: 3, quantity: 5 },
  { id: 4, productId: 2, quantity: 3 },
  { id: 5, productId: 1, quantity: 4 },
  { id: 6, productId: 3, quantity: 2 },
  { id: 7, productId: 4, quantity: 1 },
  { id: 8, productId: 2, quantity: 2 },
  { id: 9, productId: 3, quantity: 3 },
  { id: 10, productId: 1, quantity: 1 }
];

const products = [
  { id: 1, name: 'Apple', price: 1000 },
  { id: 2, name: 'Banana', price: 500 },
  { id: 3, name: 'Orange', price: 800 },
  { id: 4, name: 'Grapes', price: 1500 }
];

출력값

8500
hint: find() 함수
products.find(product => product.id === order.productId)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant