Skip to content

Commit 05372de

Browse files
Solve : reduceNameNickname
1 parent 82daf7e commit 05372de

File tree

9 files changed

+151
-0
lines changed

9 files changed

+151
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# 문제제목
2+
3+
## 설명
4+
5+
reduce 메소드를 사용해서 배열의 모든 수의 합을 구하세요.
6+
7+
## Expected Output
8+
9+
106

Challenge/jybaek96/reduceSum/solve.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// write your codes
2+
function solution(inputArray) {
3+
return inputArray.reduce((pre, acc) => pre + acc, 0);
4+
}
5+
exports.solution = solution;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const { solution } = require('./solve');
2+
3+
const test1 = {
4+
input: [10, 3, 20, 5, 8, 60],
5+
answer: 106,
6+
};
7+
8+
describe('reduceSum', () => {
9+
test('test1', () => {
10+
expect(solution(test1.input)).toEqual(test1.answer);
11+
});
12+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# 문제제목
2+
3+
## 설명
4+
5+
배열안의 객체를 price를 기준으로 오름차순 정렬한 배열을 출력하세요
6+
7+
## Expected Output
8+
9+
[
10+
{ name: '사과', price: 1000 },
11+
{ name: '당근', price: 2000 },
12+
{ name: '수박', price: 5000 },
13+
{ name: '참외', price: 10000 }
14+
]
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// write your codes
2+
function solution(inputArray) {
3+
return inputArray.sort((a,b) => a.price - b.price);
4+
}
5+
6+
exports.solution = solution;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const { solution } = require('./solve');
2+
3+
const test1 = {
4+
input: [
5+
{
6+
name: '사과',
7+
price: 1000,
8+
},
9+
{
10+
name: '수박',
11+
price: 5000,
12+
},
13+
{
14+
name: '당근',
15+
price: 2000,
16+
},
17+
{
18+
name: '참외',
19+
price: 10000,
20+
},
21+
],
22+
answer: [
23+
{ name: '사과', price: 1000 },
24+
{ name: '당근', price: 2000 },
25+
{ name: '수박', price: 5000 },
26+
{ name: '참외', price: 10000 },
27+
],
28+
};
29+
30+
describe('sortByPrice', () => {
31+
test('test1', () => {
32+
expect(solution(test1.input)).toEqual(test1.answer);
33+
});
34+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# 문제제목
2+
3+
## 설명
4+
5+
배열안의 객체를 price를 기준으로 오름차순 정렬한 배열을 출력하세요
6+
만약 price가 같다면 quantity기준으로 오름차순 정렬하세요
7+
8+
## Expected Output
9+
10+
[
11+
{ name: '사과', price: 1000, quantity: 2 },
12+
{ name: '오이', price: 2000, quantity: 49 },
13+
{ name: '당근', price: 2000, quantity: 50 },
14+
{ name: '참외', price: 5000, quantity: 10 },
15+
{ name: '수박', price: 5000, quantity: 20 }
16+
]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// write your codes
2+
function solution(inputArray) {
3+
return inputArray.sort((a,b) => {
4+
if(a.price === b.price){
5+
return a.quantity - b.quantity;
6+
}
7+
return a.price - b.price;
8+
});
9+
}
10+
11+
exports.solution = solution;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const { solution } = require('./solve');
2+
3+
const test1 = {
4+
input: [
5+
{
6+
name: '사과',
7+
price: 1000,
8+
quantity: 2,
9+
},
10+
{
11+
name: '수박',
12+
price: 5000,
13+
quantity: 20,
14+
},
15+
{
16+
name: '당근',
17+
price: 2000,
18+
quantity: 50,
19+
},
20+
{
21+
name: '참외',
22+
price: 5000,
23+
quantity: 10,
24+
},
25+
{
26+
name: '오이',
27+
price: 2000,
28+
quantity: 49,
29+
},
30+
],
31+
answer: [
32+
{ name: '사과', price: 1000, quantity: 2 },
33+
{ name: '오이', price: 2000, quantity: 49 },
34+
{ name: '당근', price: 2000, quantity: 50 },
35+
{ name: '참외', price: 5000, quantity: 10 },
36+
{ name: '수박', price: 5000, quantity: 20 },
37+
],
38+
};
39+
40+
describe('sortByPriceAndQuantity', () => {
41+
test('test1', () => {
42+
expect(solution(test1.input)).toEqual(test1.answer);
43+
});
44+
});

0 commit comments

Comments
 (0)