diff --git "a/\354\226\270\354\204\235/week10/1.js" "b/\354\226\270\354\204\235/week10/1.js" new file mode 100644 index 0000000..eaee6c1 --- /dev/null +++ "b/\354\226\270\354\204\235/week10/1.js" @@ -0,0 +1,12 @@ +// https://school.programmers.co.kr/learn/courses/30/lessons/12937 +// 짝수와 홀수 + +function solution(num) { + return num % 2 === 0 ? 'Even' : 'Odd'; +} + +// 다른 사람 풀이 + +function evenOrOdd(num) { + return num % 2 ? "Odd" : "Even" +} diff --git "a/\354\226\270\354\204\235/week10/2.js" "b/\354\226\270\354\204\235/week10/2.js" new file mode 100644 index 0000000..c061e05 --- /dev/null +++ "b/\354\226\270\354\204\235/week10/2.js" @@ -0,0 +1,8 @@ +// https://school.programmers.co.kr/learn/courses/30/lessons/12910 +// 나누어 떨어지는 숫자 배열 + +function solution(arr, divisor) { + const result = arr.filter((num) => !(num % divisor)); + + return result.length ? result.sort((a, b) => a - b) : [-1]; +} \ No newline at end of file diff --git "a/\354\226\270\354\204\235/week10/3.js" "b/\354\226\270\354\204\235/week10/3.js" new file mode 100644 index 0000000..0b25885 --- /dev/null +++ "b/\354\226\270\354\204\235/week10/3.js" @@ -0,0 +1,8 @@ +// https://school.programmers.co.kr/learn/courses/30/lessons/12919 +// 서울에서 김서방 찾기 + +function solution(seoul) { + return `김서방은 ${seoul.indexOf('Kim')}에 있다` +} + +console.log(solution(['Jane', 'Kim'])); diff --git "a/\354\226\270\354\204\235/week10/4.js" "b/\354\226\270\354\204\235/week10/4.js" new file mode 100644 index 0000000..bc0d3b0 --- /dev/null +++ "b/\354\226\270\354\204\235/week10/4.js" @@ -0,0 +1,8 @@ +// https://school.programmers.co.kr/learn/courses/30/lessons/12933 +// 정수 내림차순으로 배치하기 + +function solution(n) { + const arr = String(n).split(''); + + return Number(arr.sort().reverse().join('')); +} diff --git "a/\354\226\270\354\204\235/week8/1.js" "b/\354\226\270\354\204\235/week8/1.js" new file mode 100644 index 0000000..499b826 --- /dev/null +++ "b/\354\226\270\354\204\235/week8/1.js" @@ -0,0 +1,15 @@ +// https://school.programmers.co.kr/learn/courses/30/lessons/12935 +// 제일 작은 수 제거하기 + +function solution(arr) { + const minIdx = arr.indexOf(Math.min(...arr)); + console.log(minIdx); + + arr.splice(minIdx, 1) + + if (arr.length === 0) { + return [-1] + } + + return arr; +} diff --git "a/\354\226\270\354\204\235/week8/2.js" "b/\354\226\270\354\204\235/week8/2.js" new file mode 100644 index 0000000..3d1e7e8 --- /dev/null +++ "b/\354\226\270\354\204\235/week8/2.js" @@ -0,0 +1,38 @@ +// https://school.programmers.co.kr/learn/courses/30/lessons/12903 +// 가운데 글자 가져오기 + +function solution(s) { + const strLen = s.length; + + const midIdx = Math.floor(strLen / 2); + + if (strLen % 2 !== 0) return s[midIdx]; + if (strLen % 2 === 0) return s[midIdx - 1] + s[midIdx]; + + return midIdx; +} + +// 다른 사람 풀이 + +function solution2(s) { + return s.substr(Math.ceil(s.length / 2) - 1, s.length % 2 === 0 ? 2 : 1); +} + +/** + * substr() + * 사용을 권장하진 않는다고 한다. + * + * substr(start) + * substr(start, length) + * + * return vale + * 해당 부분의 새로운 string + * + * const str = 'Mozilla'; + * + * console.log(str.substr(1, 2)); + * // Expected output: "oz" + * + * console.log(str.substr(2)); + * // Expected output: "zilla" + */ \ No newline at end of file diff --git "a/\354\226\270\354\204\235/week8/3.js" "b/\354\226\270\354\204\235/week8/3.js" new file mode 100644 index 0000000..e032cad --- /dev/null +++ "b/\354\226\270\354\204\235/week8/3.js" @@ -0,0 +1,28 @@ +// https://school.programmers.co.kr/learn/courses/30/lessons/12917 +// 문자열 내림차순으로 배치하기 + +function solution(s) { + return (Array(...s).sort().reverse().join('')); +} + +// 다른 풀이 +function solution2(s) { + return s + .split("") + .sort() + .reverse() + .join(""); +} + +/** + * obj.sort().reverse(); + * vs + * obj.sort((a, b) => (a > b ? -1 : 1)) + * vs + * obj.sort((a, b) => b.localeCompare(a)) + * + * winner is obj.sort().reverse() + * + * ref + * https://stackoverflow.com/questions/52030110/sorting-strings-in-descending-order-in-javascript-most-efficiently + */ \ No newline at end of file diff --git "a/\354\226\270\354\204\235/week9/1.js" "b/\354\226\270\354\204\235/week9/1.js" new file mode 100644 index 0000000..fb7fe4f --- /dev/null +++ "b/\354\226\270\354\204\235/week9/1.js" @@ -0,0 +1,17 @@ +// https://school.programmers.co.kr/learn/courses/30/lessons/12948 +// 핸드폰 번호 가리기 + +function solution(phone_number) { + return '*'.repeat(phone_number.length - 4) + phone_number.slice(phone_number.length - 4); +} + +// 다른 사람 풀이1 +function hide_numbers(s){ + return s.replace(/\d(?=\d{4})/g, "*"); +} + +// 다른 사람 풀이2 +function hide_numbers(s){ + var result = "*".repeat(s.length - 4) + s.slice(-4); + return result; +} \ No newline at end of file diff --git "a/\354\226\270\354\204\235/week9/2.js" "b/\354\226\270\354\204\235/week9/2.js" new file mode 100644 index 0000000..a17d649 --- /dev/null +++ "b/\354\226\270\354\204\235/week9/2.js" @@ -0,0 +1,24 @@ +// https://school.programmers.co.kr/learn/courses/30/lessons/12912 +// 두 정수 사이의 합 + +function solution(a, b) { + let sum = 0; + if (a < b) { + for (let i = a; i <= b; i++) { + sum += i; + } + } else { + for (let i = b; i <= a; i++) { + sum += i; + } + } + + return sum; +} + +// 다른 사람 풀이 +function adder(a, b, s = 0){ + for (var i = Math.min(a, b); i <= Math.max(a, b); i++) s += i; + + return s; +} diff --git "a/\354\226\270\354\204\235/week9/3.js" "b/\354\226\270\354\204\235/week9/3.js" new file mode 100644 index 0000000..c7eb5c9 --- /dev/null +++ "b/\354\226\270\354\204\235/week9/3.js" @@ -0,0 +1,12 @@ +// https://school.programmers.co.kr/learn/courses/30/lessons/86051 +// 없는 숫자 더하기 + +function solution(numbers) { + let answer = 0; + + for (let i = 0; i < 10; i++) { + if (!numbers.includes(i)) answer += i; + } + + return answer; +} diff --git "a/\354\226\270\354\204\235/week9/4.js" "b/\354\226\270\354\204\235/week9/4.js" new file mode 100644 index 0000000..9038446 --- /dev/null +++ "b/\354\226\270\354\204\235/week9/4.js" @@ -0,0 +1,8 @@ +// https://school.programmers.co.kr/learn/courses/30/lessons/12931 +// 자릿수 더하기 + +function solution(n) { + let answer = String(n).split('').reduce((acc, cur) => +acc + +cur, 0); + + return answer; +}