diff --git a/Challenge/yuzunsang/everyArray/README.md b/Challenge/yuzunsang/everyArray/README.md new file mode 100644 index 00000000..442558f6 --- /dev/null +++ b/Challenge/yuzunsang/everyArray/README.md @@ -0,0 +1,9 @@ +# 문제제목 + +## 설명 + +every를 이용해서 모든 원소가 짝수인지 아닌지를 판별하세요 + +## Expected Output + +true \ No newline at end of file diff --git a/Challenge/yuzunsang/everyArray/solve.js b/Challenge/yuzunsang/everyArray/solve.js new file mode 100644 index 00000000..b08e33b5 --- /dev/null +++ b/Challenge/yuzunsang/everyArray/solve.js @@ -0,0 +1,7 @@ +function solution(inputArray) { + const result = inputArray.every((el) => el % 2 === 0); + + return result; +} + +exports.solution = solution; diff --git a/Challenge/yuzunsang/everyArray/solve.test.js b/Challenge/yuzunsang/everyArray/solve.test.js new file mode 100644 index 00000000..7dac26a4 --- /dev/null +++ b/Challenge/yuzunsang/everyArray/solve.test.js @@ -0,0 +1,21 @@ +const { solution } = require("./solve"); + +const test1 = { + input: [2, 4, 6, 8, 10], + answer: true, +}; + +const test2 = { + input: [2, 3, 6, 8, 10], + answer: false, +}; + +describe("everyArray", () => { + test("모두 짝수면 true여야 한다.", () => { + expect(solution(test1.input)).toEqual(test1.answer); + }); + + test("홀수가 있으면 false여야 한다.", () => { + expect(solution(test2.input)).toEqual(test2.answer); + }); +}); diff --git a/Challenge/yuzunsang/expDivOdd/README.md b/Challenge/yuzunsang/expDivOdd/README.md new file mode 100644 index 00000000..c07ab10b --- /dev/null +++ b/Challenge/yuzunsang/expDivOdd/README.md @@ -0,0 +1,3 @@ +## 설명 + +제곱한 후 3으로 나눈 나머지가 홀수인 것 을 뽑은 배열의 총 합을 구하세요. diff --git a/Challenge/yuzunsang/expDivOdd/solve.js b/Challenge/yuzunsang/expDivOdd/solve.js new file mode 100644 index 00000000..0cba14ac --- /dev/null +++ b/Challenge/yuzunsang/expDivOdd/solve.js @@ -0,0 +1,13 @@ +function solution (inputArray) { + let ansArray = []; + + for (let i = 0; i < inputArray.length; i++) { + inputArray[i] **= 2; + + if ((inputArray[i] % 3) % 2 === 1) ansArray.push(inputArray[i]); + } + + return ansArray.reduce((a, b) => a + b); +} + +exports.solution = solution; diff --git a/Challenge/yuzunsang/expDivOdd/solve.test.js b/Challenge/yuzunsang/expDivOdd/solve.test.js new file mode 100644 index 00000000..ca37fcc3 --- /dev/null +++ b/Challenge/yuzunsang/expDivOdd/solve.test.js @@ -0,0 +1,21 @@ +const { solution } = require('./solve'); + +const test1 = { + input: [1, 7, 3, 4, 6], + answer: 66, +}; + +const test2 = { + input: [2, 3, 6, 8, 10], + answer: 168, +}; + +describe('everyArray', () => { + test('1, 7, 3, 4, 6', () => { + expect(solution(test1.input)).toEqual(test1.answer); + }); + + test('2, 3, 6, 8, 10', () => { + expect(solution(test2.input)).toEqual(test2.answer); + }); +}); diff --git a/Challenge/yuzunsang/filterAge/README.md b/Challenge/yuzunsang/filterAge/README.md new file mode 100644 index 00000000..873a15c1 --- /dev/null +++ b/Challenge/yuzunsang/filterAge/README.md @@ -0,0 +1,3 @@ +## 설명 + +배열 원소의 age가 30이상 50미만인 사람만 있는 객체의 배열을 만드세요 diff --git a/Challenge/yuzunsang/filterAge/solve.js b/Challenge/yuzunsang/filterAge/solve.js new file mode 100644 index 00000000..4d68fd9a --- /dev/null +++ b/Challenge/yuzunsang/filterAge/solve.js @@ -0,0 +1,7 @@ +// write your codes +function solution(inputArray) { + const result = inputArray.filter((item) => item.age >= 30 && item.age < 50); + return result; +} + +exports.solution = solution; \ No newline at end of file diff --git a/Challenge/yuzunsang/filterAge/solve.test.js b/Challenge/yuzunsang/filterAge/solve.test.js new file mode 100644 index 00000000..b9081601 --- /dev/null +++ b/Challenge/yuzunsang/filterAge/solve.test.js @@ -0,0 +1,32 @@ +const { solution } = require('./solve'); + +const test1 = { + input: [ + { + name: '영미', + age: 25, + }, + { + name: '일미', + age: 35, + }, + { + name: '이미', + age: 45, + }, + { + name: '삼미', + age: 55, + }, + ], + answer: [ + { name: '일미', age: 35 }, + { name: '이미', age: 45 }, + ], +}; + +describe('filterAge', () => { + test('test1', () => { + expect(solution(test1.input)).toEqual(test1.answer); + }); +}); diff --git a/Challenge/yuzunsang/filterIntersection/README.md b/Challenge/yuzunsang/filterIntersection/README.md new file mode 100644 index 00000000..d7c9ad39 --- /dev/null +++ b/Challenge/yuzunsang/filterIntersection/README.md @@ -0,0 +1,5 @@ +# 문제제목 + +## 설명 + +두 배열의 교집합을 출력하세요! diff --git a/Challenge/yuzunsang/filterIntersection/solve.js b/Challenge/yuzunsang/filterIntersection/solve.js new file mode 100644 index 00000000..20bdb691 --- /dev/null +++ b/Challenge/yuzunsang/filterIntersection/solve.js @@ -0,0 +1,7 @@ +function solution (inputArray1, inputArray2) { + // filter는 배열에서 조건에 맞는 원소만을 남겨둘 때 사용 + // includes는 어떠한 원소가 배열에 포함되어 있는 지를 boolean으로 반환 + return inputArray1.filter((el) => inputArray2.includes(el)); +} + +exports.solution = solution; diff --git a/Challenge/yuzunsang/filterIntersection/solve.test.js b/Challenge/yuzunsang/filterIntersection/solve.test.js new file mode 100644 index 00000000..576cbc86 --- /dev/null +++ b/Challenge/yuzunsang/filterIntersection/solve.test.js @@ -0,0 +1,15 @@ +const { solution } = require('./solve'); + +const test1 = { + input: { + A: [1, 2, 3, 4, 5], + B: [3, 4, 5, 6, 7], + }, + answer: [3, 4, 5], +}; + +describe('filterIntersection', () => { + test('test1', () => { + expect(solution(test1.input.A, test1.input.B)).toEqual(test1.answer); + }); +}); diff --git a/Challenge/yuzunsang/filterOdd/README.md b/Challenge/yuzunsang/filterOdd/README.md new file mode 100644 index 00000000..cf9eda79 --- /dev/null +++ b/Challenge/yuzunsang/filterOdd/README.md @@ -0,0 +1,3 @@ +## 설명 + +홀수만 뽑아 배열로 만드세요 diff --git a/Challenge/yuzunsang/filterOdd/solve.js b/Challenge/yuzunsang/filterOdd/solve.js new file mode 100644 index 00000000..602d3415 --- /dev/null +++ b/Challenge/yuzunsang/filterOdd/solve.js @@ -0,0 +1,8 @@ +// write your codes + +function solution(inputArray) { + // filter는 배열에서 조건에 맞는 원소만 남겨두는 것 + return inputArray.filter((v) => v % 2 === 1); +} + +exports.solution = solution; diff --git a/Challenge/yuzunsang/filterOdd/solve.test.js b/Challenge/yuzunsang/filterOdd/solve.test.js new file mode 100644 index 00000000..8cfd7f5a --- /dev/null +++ b/Challenge/yuzunsang/filterOdd/solve.test.js @@ -0,0 +1,28 @@ +const { solution } = require('./solve'); + +const test1 = { + input: [4, 2, 5, 1, 3], + answer: [5, 1, 3], +}; + +const test2 = { + input: [4, 2, 6, 8, 50, 16], + answer: [], +}; + +const test3 = { + input: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], + answer: [1, 3, 5, 7, 9, 11], +}; + +describe('filterOdd', () => { + test('test1', () => { + expect(solution(test1.input)).toEqual(test1.answer); + }); + test('test2', () => { + expect(solution(test2.input)).toEqual(test2.answer); + }); + test('test3', () => { + expect(solution(test3.input)).toEqual(test3.answer); + }); +}); diff --git a/Challenge/yuzunsang/findWord/README.md b/Challenge/yuzunsang/findWord/README.md new file mode 100644 index 00000000..8ef123f4 --- /dev/null +++ b/Challenge/yuzunsang/findWord/README.md @@ -0,0 +1,9 @@ +# 문제제목 + +## 설명 + +용가리라는 단어가 있으면 true 없으면 false를 출력 + +## Expected Output + +true diff --git a/Challenge/yuzunsang/findWord/solve.js b/Challenge/yuzunsang/findWord/solve.js new file mode 100644 index 00000000..238bcc8a --- /dev/null +++ b/Challenge/yuzunsang/findWord/solve.js @@ -0,0 +1,6 @@ +// write your codes +function solution(inputArray) { + return inputArray.includes('용가리'); +} + +exports.solution = solution; diff --git a/Challenge/yuzunsang/findWord/solve.test.js b/Challenge/yuzunsang/findWord/solve.test.js new file mode 100644 index 00000000..804114f6 --- /dev/null +++ b/Challenge/yuzunsang/findWord/solve.test.js @@ -0,0 +1,26 @@ +const { solution } = require('./solve'); + +const test1 = { + input: ['잠', '자', '고', '싶', '다', '용가리'], + answer: true, +}; +const test2 = { + input: ['맛있는', '용가리치킨'], + answer: false, +}; +const test3 = { + input: ['고질라', '용가리 ', '울트라맨'], + answer: false, +}; + +describe('findWord', () => { + test('test1', () => { + expect(solution(test1.input)).toEqual(test1.answer); + }); + test('test2', () => { + expect(solution(test2.input)).toEqual(test2.answer); + }); + test('test3', () => { + expect(solution(test3.input)).toEqual(test3.answer); + }); +}); \ No newline at end of file diff --git a/Challenge/yuzunsang/forEachFilter/README.md b/Challenge/yuzunsang/forEachFilter/README.md new file mode 100644 index 00000000..6dcbc7c2 --- /dev/null +++ b/Challenge/yuzunsang/forEachFilter/README.md @@ -0,0 +1,3 @@ +## 설명 + +배열 원소중 40 이상인 수만 뽑아 배열을 만드세요. diff --git a/Challenge/yuzunsang/forEachFilter/solve.js b/Challenge/yuzunsang/forEachFilter/solve.js new file mode 100644 index 00000000..efb28746 --- /dev/null +++ b/Challenge/yuzunsang/forEachFilter/solve.js @@ -0,0 +1,6 @@ +// write your codes +function solution(inputArray) { + return inputArray.filter((el) => el >= 40); +} + +exports.solution = solution; diff --git a/Challenge/yuzunsang/forEachFilter/solve.test.js b/Challenge/yuzunsang/forEachFilter/solve.test.js new file mode 100644 index 00000000..3d73b25c --- /dev/null +++ b/Challenge/yuzunsang/forEachFilter/solve.test.js @@ -0,0 +1,12 @@ +const { solution } = require('./solve'); + +const test1 = { + input: [100, 10, 20, 40], + answer: [100, 40], +}; + +describe('forEachFilter', () => { + test('test1', () => { + expect(solution(test1.input)).toEqual(test1.answer); + }); +}); diff --git a/Challenge/yuzunsang/forEachFilterIsNaN/README.md b/Challenge/yuzunsang/forEachFilterIsNaN/README.md new file mode 100644 index 00000000..570c030e --- /dev/null +++ b/Challenge/yuzunsang/forEachFilterIsNaN/README.md @@ -0,0 +1,3 @@ +## 설명 + +배열 원소중 숫자인 원소만 뽑아 배열을 만드세요. diff --git a/Challenge/yuzunsang/forEachFilterIsNaN/solve.js b/Challenge/yuzunsang/forEachFilterIsNaN/solve.js new file mode 100644 index 00000000..0b204fe9 --- /dev/null +++ b/Challenge/yuzunsang/forEachFilterIsNaN/solve.js @@ -0,0 +1,6 @@ +// write your codes +function solution(inputArray) { + return inputArray.filter((el) => typeof el === 'number'); +} + +exports.solution = solution; diff --git a/Challenge/yuzunsang/forEachFilterIsNaN/solve.test.js b/Challenge/yuzunsang/forEachFilterIsNaN/solve.test.js new file mode 100644 index 00000000..d6e118bb --- /dev/null +++ b/Challenge/yuzunsang/forEachFilterIsNaN/solve.test.js @@ -0,0 +1,28 @@ +const { solution } = require('./solve'); + +const test1 = { + input: [1, 40, '라매', '개발자', 51.5, 'a', 88], + answer: [1, 40, 51.5, 88], +}; + +const test2 = { + input: [1, 2, 3, '4', 5, '6'], + answer: [1, 2, 3, 5], +}; + +const test3 = { + input: [-3, -2, -1, 0, 1, 2, 3], + answer: [-3, -2, -1, 0, 1, 2, 3], +}; + +describe('forEachFilterIsNaN', () => { + test('test1: 숫자,문자 판별', () => { + expect(solution(test1.input)).toEqual(test1.answer); + }); + test('test2: 문자로된 숫자 판별', () => { + expect(solution(test2.input)).toEqual(test2.answer); + }); + test('test3: 음수 양수 포함', () => { + expect(solution(test3.input)).toEqual(test3.answer); + }); +}); \ No newline at end of file diff --git a/Challenge/yuzunsang/forEachMap/README.md b/Challenge/yuzunsang/forEachMap/README.md new file mode 100644 index 00000000..1e8e03c8 --- /dev/null +++ b/Challenge/yuzunsang/forEachMap/README.md @@ -0,0 +1,3 @@ +## 설명 + +forEach 메소드를 사용해서 배열의 각 원소 끝에 '%'를 붙인 문자열 배열을 출력하세요 diff --git a/Challenge/yuzunsang/forEachMap/solve.js b/Challenge/yuzunsang/forEachMap/solve.js new file mode 100644 index 00000000..4d934950 --- /dev/null +++ b/Challenge/yuzunsang/forEachMap/solve.js @@ -0,0 +1,10 @@ +// write your codes +function solution(inputArray) { + const answer = []; + + inputArray.forEach((item) => answer.push(`${item}%`)); + + return answer; +} + +exports.solution = solution; diff --git a/Challenge/yuzunsang/forEachMap/solve.test.js b/Challenge/yuzunsang/forEachMap/solve.test.js new file mode 100644 index 00000000..de4868c6 --- /dev/null +++ b/Challenge/yuzunsang/forEachMap/solve.test.js @@ -0,0 +1,12 @@ +const { solution } = require('./solve'); + +const test1 = { + input: [100, 10, 20, 40], + answer: ['100%', '10%', '20%', '40%'], +}; + +describe('forEachMap', () => { + test('test1', () => { + expect(solution(test1.input)).toEqual(test1.answer); + }); +}); diff --git a/Challenge/yuzunsang/forEachReduce/README.md b/Challenge/yuzunsang/forEachReduce/README.md new file mode 100644 index 00000000..a984df2d --- /dev/null +++ b/Challenge/yuzunsang/forEachReduce/README.md @@ -0,0 +1,3 @@ +## 설명 + +forEach 메소드를 사용해서 배열의 총 합을 출력하는 코드를 작성하세요 diff --git a/Challenge/yuzunsang/forEachReduce/solve.js b/Challenge/yuzunsang/forEachReduce/solve.js new file mode 100644 index 00000000..48e350fe --- /dev/null +++ b/Challenge/yuzunsang/forEachReduce/solve.js @@ -0,0 +1,10 @@ +// write your codes +function solution(inputArray) { + let sum = 0; + + inputArray.forEach((item) => sum += item); + + return sum; +} + +exports.solution = solution; diff --git a/Challenge/yuzunsang/forEachReduce/solve.test.js b/Challenge/yuzunsang/forEachReduce/solve.test.js new file mode 100644 index 00000000..397e4b50 --- /dev/null +++ b/Challenge/yuzunsang/forEachReduce/solve.test.js @@ -0,0 +1,28 @@ +const { solution } = require('./solve'); + +const test1 = { + input: [100, 10, 20, 40], + answer: 170, +}; + +const test2 = { + input: [120, -20, -30, 0, 15], + answer: 85, +}; + +const test3 = { + input: [-10, -20, -30], + answer: -60, +}; + +describe('forEachReduce', () => { + test('test1', () => { + expect(solution(test1.input)).toEqual(test1.answer); + }); + test('test2: 음수가 포함된 계산', () => { + expect(solution(test2.input)).toEqual(test2.answer); + }); + test('test3: 음수만 존재하는 계산', () => { + expect(solution(test3.input)).toEqual(test3.answer); + }); +}); diff --git a/Challenge/yuzunsang/mapAddPercent/README.md b/Challenge/yuzunsang/mapAddPercent/README.md new file mode 100644 index 00000000..75e73042 --- /dev/null +++ b/Challenge/yuzunsang/mapAddPercent/README.md @@ -0,0 +1,3 @@ +## 설명 + +map 메소드를 사용해 배열 각각 숫자 뒤에 %를 붙인 문자열을 만드세요 diff --git a/Challenge/yuzunsang/mapAddPercent/solve.js b/Challenge/yuzunsang/mapAddPercent/solve.js new file mode 100644 index 00000000..f9832889 --- /dev/null +++ b/Challenge/yuzunsang/mapAddPercent/solve.js @@ -0,0 +1,6 @@ +// write your codes +function solution(inputArray) { + return inputArray.map((item) => `${item}%`); +} + +exports.solution = solution; diff --git a/Challenge/yuzunsang/mapAddPercent/solve.test.js b/Challenge/yuzunsang/mapAddPercent/solve.test.js new file mode 100644 index 00000000..dac1b3cc --- /dev/null +++ b/Challenge/yuzunsang/mapAddPercent/solve.test.js @@ -0,0 +1,12 @@ +const { solution } = require('./solve'); + +const test1 = { + input: [100, 10, 20, 40], + answer: ['100%', '10%', '20%', '40%'], +}; + +describe('mapAddPercent', () => { + test('test1', () => { + expect(solution(test1.input)).toEqual(test1.answer); + }); +}); diff --git a/Challenge/yuzunsang/mapAppendOrder/README.md b/Challenge/yuzunsang/mapAppendOrder/README.md new file mode 100644 index 00000000..1435623a --- /dev/null +++ b/Challenge/yuzunsang/mapAppendOrder/README.md @@ -0,0 +1,3 @@ +## 설명 + +배열의 값을 name 프로퍼티에 넣고 몇번째 원소인지를 order에 넣은 객체의 배열을 출력하세요 diff --git a/Challenge/yuzunsang/mapAppendOrder/solve.js b/Challenge/yuzunsang/mapAppendOrder/solve.js new file mode 100644 index 00000000..e7f8b51c --- /dev/null +++ b/Challenge/yuzunsang/mapAppendOrder/solve.js @@ -0,0 +1,10 @@ +// write your codes +function solution(inputArray) { + let answer = []; + + inputArray.map((el, idx) => answer.push({ name : el, order : idx + 1 })); + + return answer; +} + +exports.solution = solution; diff --git a/Challenge/yuzunsang/mapAppendOrder/solve.test.js b/Challenge/yuzunsang/mapAppendOrder/solve.test.js new file mode 100644 index 00000000..e08db10c --- /dev/null +++ b/Challenge/yuzunsang/mapAppendOrder/solve.test.js @@ -0,0 +1,16 @@ +const { solution } = require('./solve'); + +const test1 = { + input: ['홍길동', '둘리', '루피'], + answer: [ + { name: '홍길동', order: 1 }, + { name: '둘리', order: 2 }, + { name: '루피', order: 3 }, + ], +}; + +describe('mapAppendOrder', () => { + test('test1', () => { + expect(solution(test1.input)).toEqual(test1.answer); + }); +}); diff --git a/Challenge/yuzunsang/reduceMaxValueNIndex/README.md b/Challenge/yuzunsang/reduceMaxValueNIndex/README.md new file mode 100644 index 00000000..965bdec6 --- /dev/null +++ b/Challenge/yuzunsang/reduceMaxValueNIndex/README.md @@ -0,0 +1,3 @@ +## 설명 + +reduce 메소드를 사용해 최댓값의 값을 maxValue에, 해당 값의 index를 idx에 넣은 객체를 출력하세요 diff --git a/Challenge/yuzunsang/reduceMaxValueNIndex/practice.js b/Challenge/yuzunsang/reduceMaxValueNIndex/practice.js new file mode 100644 index 00000000..e817c378 --- /dev/null +++ b/Challenge/yuzunsang/reduceMaxValueNIndex/practice.js @@ -0,0 +1,95 @@ +// 1. 숫자 배열의 합 구하기 +// const numbers = [1, 2, 3, 4, 5]; + +// const sum = numbers.reduce((acc, cur) => { +// return acc + cur; +// }, 0); + +// console.log(sum); + +// 2. 객체 배열에서 나이의 총합 구하기 +// const users = [ +// { name: "Kim", age: 16 }, +// { name: "Lee", age: 25 }, +// { name: "Park", age: 30 }, +// { name: "Choi", age: 14 }, +// { name: "Jeon", age: 19 }, +// ]; + +// const totalAge = users.reduce((acc, cur) => { +// return acc + cur.age; +// }, 0); + +// console.log(totalAge); + +// 3. 문자열 배열을 하나의 문자열로 합치기 +// const words = ["Hello", " ", "World", "!"]; + +// const sentence = words.reduce((acc, cur) => { +// acc += cur; +// return acc; +// }, ""); + +// console.log(sentence); + +// 4. 배열의 최댓값 찾기 +// const numbers = [10, 3, 20, 15, 7]; + +// const findMax = numbers.reduce((acc, cur) => { +// return Math.max(acc, cur); +// }, numbers[0]); + +// console.log(findMax); + +// 5. 각 문자열의 등장 횟수 세기 +// const words = ["apple", "banana", "apple", "orange", "banana", "orange", "apple"]; + +// const wordCounter = words.reduce((acc, cur) => { +// // acc[cur]에서 cur가 key 가 되고, acc[cur]는 value가 됨. +// acc[cur] = (acc[cur] || 0) + 1; + +// return acc; +// } +// , {}); + +// console.log(wordCounter); + +// 6. 객체 배열에서 각 카테고리별 요소 개수 세기 +const items = [ + { name: "A", category: "X" }, + { name: "B", category: "Y" }, + { name: "C", category: "X" }, + { name: "D", category: "Z" }, + { name: "E", category: "Y" }, + { name: "F", category: "X" }, + ]; + + const cntItemsByCategories = items.reduce((acc, cur) => { + acc[cur.category] = (acc[cur.category] || 0) + 1; + + return acc; + }, {}); + + console.log(cntItemsByCategories); + +// console.log(cntItemsByCategories); + +// 7. 중첩 배열을 단일 배열로 평탄화하기 +// const nestedArrays = [[1, 2], [3, 4], [5, 6], [7, 8]]; + +// const normalizeToOne = nestedArrays.reduce((acc, cur) => { +// return acc.concat(cur); +// }, []); + +// console.log(normalizeToOne); + +// 8. initialValue 를 사용하는 예제: 숫자 배열에서 각 숫자의 제곱을 담은 새 배열 생성하기 + +// const numbers = [1, 2, 3, 4, 5]; + +// const result = numbers.reduce((acc, cur) => { +// acc.push(cur ** 2); +// return acc; +// }, []); + +// console.log(result); \ No newline at end of file diff --git a/Challenge/yuzunsang/reduceMaxValueNIndex/solve.js b/Challenge/yuzunsang/reduceMaxValueNIndex/solve.js new file mode 100644 index 00000000..857c03e6 --- /dev/null +++ b/Challenge/yuzunsang/reduceMaxValueNIndex/solve.js @@ -0,0 +1,11 @@ +// write your codes +function solution(inputArray) { + return result = inputArray.reduce((acc, cur, index) => { + if (acc.maxValue < cur) { + return { maxValue : cur, idx : index }; + } + return acc; + } + , { maxValue: -Infinity, idx: -1 })}; + +exports.solution = solution; \ No newline at end of file diff --git a/Challenge/yuzunsang/reduceMaxValueNIndex/solve.test.js b/Challenge/yuzunsang/reduceMaxValueNIndex/solve.test.js new file mode 100644 index 00000000..0daef5b8 --- /dev/null +++ b/Challenge/yuzunsang/reduceMaxValueNIndex/solve.test.js @@ -0,0 +1,28 @@ +const { solution } = require('./solve'); + +const test1 = { + input: [3, 29, 38, 12, 57, 74, 40, 85, 61], + answer: { maxValue: 85, idx: 7 }, +}; + +const test2 = { + input: [-24, -2, -13, -49, -999999, -17], + answer: { maxValue: -2, idx: 1 }, +}; +//최댓값이 중복일 때에는 먼저 나온 최댓값의 인덱스를 유지하도록 설정하였습니다. +const test3 = { + input: [2, -20, 21, -874, 99, -16, -29, 99], + answer: { maxValue: 99, idx: 4 }, +}; + +describe('reduceMaxValueNIndex', () => { + test('test1', () => { + expect(solution(test1.input)).toEqual(test1.answer); + }); + test('test2 : 음수 테스트', () => { + expect(solution(test2.input)).toEqual(test2.answer); + }); + test('test3 : 중복된 최대값 테스트', () => { + expect(solution(test3.input)).toEqual(test3.answer); + }); +}); diff --git a/Challenge/yuzunsang/reduceNameNickname/README.md b/Challenge/yuzunsang/reduceNameNickname/README.md new file mode 100644 index 00000000..b6920e05 --- /dev/null +++ b/Challenge/yuzunsang/reduceNameNickname/README.md @@ -0,0 +1,3 @@ +## 설명 + +입력받은 객채배열의 nickname을 key, name을 value로 하는 객체를 출력하세요 diff --git a/Challenge/yuzunsang/reduceNameNickname/solve.js b/Challenge/yuzunsang/reduceNameNickname/solve.js new file mode 100644 index 00000000..82025f58 --- /dev/null +++ b/Challenge/yuzunsang/reduceNameNickname/solve.js @@ -0,0 +1,11 @@ +// write your codes + +function solution(inputArray) { + return result = inputArray.reduce((acc, cur) => { + acc[cur.nickname] = cur.name; + // cur.nickname -> key, acc[cur.nickname] -> value + return acc; + }, {}); +} + +exports.solution = solution; diff --git a/Challenge/yuzunsang/reduceNameNickname/solve.test.js b/Challenge/yuzunsang/reduceNameNickname/solve.test.js new file mode 100644 index 00000000..67b74529 --- /dev/null +++ b/Challenge/yuzunsang/reduceNameNickname/solve.test.js @@ -0,0 +1,25 @@ +const { solution } = require('./solve'); + +const test1 = { + input: [ + { + name: '홍길동', + nickname: 'hong', + }, + { + name: '둘리', + nickname: '2li', + }, + { + name: '오스트랄로피테쿠스', + nickname: '1Cin', + }, + ], + answer: { hong: '홍길동', '2li': '둘리', '1Cin': '오스트랄로피테쿠스' }, +}; + +describe('reduceNameNickname', () => { + test('test1', () => { + expect(solution(test1.input)).toEqual(test1.answer); + }); +}); diff --git a/Challenge/yuzunsang/reduceSum/README.md b/Challenge/yuzunsang/reduceSum/README.md new file mode 100644 index 00000000..8c607eb2 --- /dev/null +++ b/Challenge/yuzunsang/reduceSum/README.md @@ -0,0 +1,9 @@ +# 문제제목 + +## 설명 + +reduce 메소드를 사용해서 배열의 모든 수의 합을 구하세요. + +## Expected Output + +106 diff --git a/Challenge/yuzunsang/reduceSum/solve.js b/Challenge/yuzunsang/reduceSum/solve.js new file mode 100644 index 00000000..17da581e --- /dev/null +++ b/Challenge/yuzunsang/reduceSum/solve.js @@ -0,0 +1,6 @@ +// write your codes +function solution(inputArray) { + return inputArray.reduce((acc, cur) => acc + cur, 0); +} + +exports.solution = solution; \ No newline at end of file diff --git a/Challenge/yuzunsang/reduceSum/solve.test.js b/Challenge/yuzunsang/reduceSum/solve.test.js new file mode 100644 index 00000000..046c40cb --- /dev/null +++ b/Challenge/yuzunsang/reduceSum/solve.test.js @@ -0,0 +1,12 @@ +const { solution } = require('./solve'); + +const test1 = { + input: [10, 3, 20, 5, 8, 60], + answer: 106, +}; + +describe('reduceSum', () => { + test('test1', () => { + expect(solution(test1.input)).toEqual(test1.answer); + }); +}); diff --git a/Challenge/yuzunsang/sortByPrice/README.md b/Challenge/yuzunsang/sortByPrice/README.md new file mode 100644 index 00000000..b1ccf70e --- /dev/null +++ b/Challenge/yuzunsang/sortByPrice/README.md @@ -0,0 +1,14 @@ +# 문제제목 + +## 설명 + +배열안의 객체를 price를 기준으로 오름차순 정렬한 배열을 출력하세요 + +## Expected Output + +[ + { name: '사과', price: 1000 }, + { name: '당근', price: 2000 }, + { name: '수박', price: 5000 }, + { name: '참외', price: 10000 } +] diff --git a/Challenge/yuzunsang/sortByPrice/practice.js b/Challenge/yuzunsang/sortByPrice/practice.js new file mode 100644 index 00000000..550d0f58 --- /dev/null +++ b/Challenge/yuzunsang/sortByPrice/practice.js @@ -0,0 +1,46 @@ +const account = [ + {id : 'jsh', addr : '서울'}, + {id : 'root2', addr : '인천'}, + {id : 'ghd', addr : '광주'}, + {id : 'root3', addr : '경기'}, + {id : 'root5', addr : '경북'}, + {id : 'rnv', addr : '세종'}, + {id : 'ekz', addr : '전북'}, + {id : 'gae', addr : '전남'}, + {id : 'root4', addr : '전남fds'}, + {id : 'root1', addr : '전남fds'} +]; + +const compare = function(a, b) { + + if ((b.id.indexOf('root') === 0 && (a.id.indexOf('root') === 0))) { + let newA = a.id.split('root'); + let newB = b.id.split('root'); + + return newA[1] - newB[1]; + // if (newA[1] > newB[1]) { + // return 1; + // } else if (newA[1] < newB[1]) { + // return -1; + // } else { + // return 0; + // } + } + else if ((a.id.indexOf('root') === 0)) { + return -1; + } + else if ((b.id.indexOf('root') === 0)) { + return 1; + } + else { + if (a.id > b.id) { + return 1; + } else if (a.id < b.id) { + return -1; + } else { + return 0; + } + } +}; + +console.log(account.sort(compare)); \ No newline at end of file diff --git a/Challenge/yuzunsang/sortByPrice/solve.js b/Challenge/yuzunsang/sortByPrice/solve.js new file mode 100644 index 00000000..3da8cbfd --- /dev/null +++ b/Challenge/yuzunsang/sortByPrice/solve.js @@ -0,0 +1,6 @@ +// write your codes +function solution(inputArray) { + return inputArray.sort((a, b) => a.price - b.price); +} + +exports.solution = solution; diff --git a/Challenge/yuzunsang/sortByPrice/solve.test.js b/Challenge/yuzunsang/sortByPrice/solve.test.js new file mode 100644 index 00000000..3d6a3431 --- /dev/null +++ b/Challenge/yuzunsang/sortByPrice/solve.test.js @@ -0,0 +1,34 @@ +const { solution } = require('./solve'); + +const test1 = { + input: [ + { + name: '사과', + price: 1000, + }, + { + name: '수박', + price: 5000, + }, + { + name: '당근', + price: 2000, + }, + { + name: '참외', + price: 10000, + }, + ], + answer: [ + { name: '사과', price: 1000 }, + { name: '당근', price: 2000 }, + { name: '수박', price: 5000 }, + { name: '참외', price: 10000 }, + ], +}; + +describe('sortByPrice', () => { + test('test1', () => { + expect(solution(test1.input)).toEqual(test1.answer); + }); +}); diff --git a/Challenge/yuzunsang/sortByPriceAndQuantity/README.md b/Challenge/yuzunsang/sortByPriceAndQuantity/README.md new file mode 100644 index 00000000..41cd5c95 --- /dev/null +++ b/Challenge/yuzunsang/sortByPriceAndQuantity/README.md @@ -0,0 +1,16 @@ +# 문제제목 + +## 설명 + +배열안의 객체를 price를 기준으로 오름차순 정렬한 배열을 출력하세요 +만약 price가 같다면 quantity기준으로 오름차순 정렬하세요 + +## Expected Output + +[ + { name: '사과', price: 1000, quantity: 2 }, + { name: '오이', price: 2000, quantity: 49 }, + { name: '당근', price: 2000, quantity: 50 }, + { name: '참외', price: 5000, quantity: 10 }, + { name: '수박', price: 5000, quantity: 20 } +] diff --git a/Challenge/yuzunsang/sortByPriceAndQuantity/solve.js b/Challenge/yuzunsang/sortByPriceAndQuantity/solve.js new file mode 100644 index 00000000..01a8cf42 --- /dev/null +++ b/Challenge/yuzunsang/sortByPriceAndQuantity/solve.js @@ -0,0 +1,13 @@ +// write your codes +function solution(inputArray) { + return inputArray.sort((a, b) => { + if (a.price !== b.price) { + return a.price - b.price; + } + else { + return a.quantity - b.quantity; + } + }); +} + +exports.solution = solution; diff --git a/Challenge/yuzunsang/sortByPriceAndQuantity/solve.test.js b/Challenge/yuzunsang/sortByPriceAndQuantity/solve.test.js new file mode 100644 index 00000000..424f42b7 --- /dev/null +++ b/Challenge/yuzunsang/sortByPriceAndQuantity/solve.test.js @@ -0,0 +1,44 @@ +const { solution } = require('./solve'); + +const test1 = { + input: [ + { + name: '사과', + price: 1000, + quantity: 2, + }, + { + name: '수박', + price: 5000, + quantity: 20, + }, + { + name: '당근', + price: 2000, + quantity: 50, + }, + { + name: '참외', + price: 5000, + quantity: 10, + }, + { + name: '오이', + price: 2000, + quantity: 49, + }, + ], + answer: [ + { name: '사과', price: 1000, quantity: 2 }, + { name: '오이', price: 2000, quantity: 49 }, + { name: '당근', price: 2000, quantity: 50 }, + { name: '참외', price: 5000, quantity: 10 }, + { name: '수박', price: 5000, quantity: 20 }, + ], +}; + +describe('sortByPriceAndQuantity', () => { + test('test1', () => { + expect(solution(test1.input)).toEqual(test1.answer); + }); +}); diff --git a/Challenge/yuzunsang/spreadOperatorMaxValue/README.md b/Challenge/yuzunsang/spreadOperatorMaxValue/README.md new file mode 100644 index 00000000..164b68a1 --- /dev/null +++ b/Challenge/yuzunsang/spreadOperatorMaxValue/README.md @@ -0,0 +1,3 @@ +## 설명 + +Spread Operator를 이용해서 값의 최대, 최소값을 구하세요 diff --git a/Challenge/yuzunsang/spreadOperatorMaxValue/solve.js b/Challenge/yuzunsang/spreadOperatorMaxValue/solve.js new file mode 100644 index 00000000..de517bc6 --- /dev/null +++ b/Challenge/yuzunsang/spreadOperatorMaxValue/solve.js @@ -0,0 +1,6 @@ +// write your codes +function solution(inputArray) { + return `max : ${Math.max(...inputArray)}, min : ${Math.min(...inputArray)}`; +} + +exports.solution = solution; diff --git a/Challenge/yuzunsang/spreadOperatorMaxValue/solve.test.js b/Challenge/yuzunsang/spreadOperatorMaxValue/solve.test.js new file mode 100644 index 00000000..26f2abf9 --- /dev/null +++ b/Challenge/yuzunsang/spreadOperatorMaxValue/solve.test.js @@ -0,0 +1,12 @@ +const { solution } = require('./solve'); + +const test1 = { + input: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], + answer: 'max : 10, min : 1', +}; + +describe('spreadOperatorMaxValue', () => { + test('test1', () => { + expect(solution(test1.input)).toEqual(test1.answer); + }); +});