Skip to content

Commit 2e14145

Browse files
authored
Merge pull request #166 from raccoon-ccoder/master
"Solve : reduceNameNickname"
2 parents db01dab + 05372de commit 2e14145

File tree

15 files changed

+229
-0
lines changed

15 files changed

+229
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## 설명
2+
3+
reduce 메소드를 사용해 최댓값의 값을 maxValue에, 해당 값의 index를 idx에 넣은 객체를 출력하세요
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// write your codes
2+
function solution(inputArray) {
3+
let obj = {};
4+
const max = inputArray.reduce((acc, cur) => acc > cur ? acc : cur);
5+
obj["maxValue"] = max;
6+
obj["idx"] = inputArray.indexOf(max);
7+
return obj;
8+
}
9+
10+
exports.solution = solution;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const { solution } = require('./solve');
2+
3+
const test1 = {
4+
input: [3, 29, 38, 12, 57, 74, 40, 85, 61],
5+
answer: { maxValue: 85, idx: 7 },
6+
};
7+
8+
const test2 = {
9+
input: [-24, -2, -13, -49, -999999, -17],
10+
answer: { maxValue: -2, idx: 1 },
11+
};
12+
//최댓값이 중복일 때에는 먼저 나온 최댓값의 인덱스를 유지하도록 설정하였습니다.
13+
const test3 = {
14+
input: [2, -20, 21, -874, 99, -16, -29, 99],
15+
answer: { maxValue: 99, idx: 4 },
16+
};
17+
18+
describe('reduceMaxValueNIndex', () => {
19+
test('test1', () => {
20+
expect(solution(test1.input)).toEqual(test1.answer);
21+
});
22+
test('test2 : 음수 테스트', () => {
23+
expect(solution(test2.input)).toEqual(test2.answer);
24+
});
25+
test('test3 : 중복된 최대값 테스트', () => {
26+
expect(solution(test3.input)).toEqual(test3.answer);
27+
});
28+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## 설명
2+
3+
입력받은 객채배열의 nickname을 key, name을 value로 하는 객체를 출력하세요
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// write your codes
2+
function solution(inputArray) {
3+
return inputArray.reduce((acc, cur) => {
4+
acc[cur.nickname] = cur.name;
5+
return acc;
6+
},{});
7+
}
8+
9+
exports.solution = solution;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const { solution } = require('./solve');
2+
3+
const test1 = {
4+
input: [
5+
{
6+
name: '홍길동',
7+
nickname: 'hong',
8+
},
9+
{
10+
name: '둘리',
11+
nickname: '2li',
12+
},
13+
{
14+
name: '오스트랄로피테쿠스',
15+
nickname: '1Cin',
16+
},
17+
],
18+
answer: { hong: '홍길동', '2li': '둘리', '1Cin': '오스트랄로피테쿠스' },
19+
};
20+
21+
describe('reduceNameNickname', () => {
22+
test('test1', () => {
23+
expect(solution(test1.input)).toEqual(test1.answer);
24+
});
25+
});
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+
]

0 commit comments

Comments
 (0)