This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const makeCompany = (showAddress) => { | |
| return { | |
| name: 'Cobalt. Inc.', | |
| ...showAddress && { address: 'Seoul' } | |
| } | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // LinkedList | |
| class Node { | |
| constructor(value) { | |
| this.value = value; | |
| this.next = null; | |
| } | |
| } | |
| class Queue { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const hour = '21'; | |
| const min = '53'; | |
| const sec = '5'; | |
| const time = `${hour}:${min}:${sec.padStart(2, '0')}` | |
| console.log(time); // 21:53:05 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let arr1 = ["it's Sunny in", "", "California"]; | |
| arr1.map(x=>x.split("")); | |
| // [["it's","Sunny","in"],[""],["California"]] | |
| arr1.flatMap(x => x.split(" ")); | |
| // ["it's","Sunny","in","","California"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Using slice, create newCar from myCar. | |
| let myHonda = { color: 'red', wheels: 4, engine: { cylinders: 4, size: 2.2 } } | |
| let myCar = [myHonda, 2, 'cherry condition', 'purchased 1997'] | |
| let newCar = myCar.slice(0, 2) | |
| // Display the values of myCar, newCar, and the color of myHonda | |
| // referenced from both arrays. | |
| console.log('myCar = ' + JSON.stringify(myCar)) | |
| console.log('newCar = ' + JSON.stringify(newCar)) | |
| console.log('myCar[0].color = ' + myCar[0].color) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function compare(a, b) { | |
| if (a is less than b by some ordering criterion) { | |
| return -1; | |
| } | |
| if (a is greater than b by the ordering criterion) { | |
| return 1; | |
| } | |
| // a must be equal to b | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class MaxHeap { | |
| constructor() { | |
| this.heap = [null]; | |
| } | |
| push(value) { | |
| this.heap.push(value); | |
| let currentIndex = this.heap.length - 1; | |
| let parentIndex = Math.floor(currentIndex / 2); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function getPrime(n) { | |
| let answer = 0; | |
| let prime = [false, false, ...Array(n - 1).fill(true)]; | |
| for(let i = 2; i * i <= n; i++) { | |
| if(prime[i]) { | |
| for(let j = i * 2; j <= n; j += i) { | |
| prime[j] = false; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function permutations(arr, n) { | |
| // 1๊ฐ๋ง ๋ฝ๋๋ค๋ฉด ๊ทธ๋๋ก ์์ด์ ๋ฐํํ๋ค. ํ์ถ ์กฐ๊ฑด์ผ๋ก๋ ์ฌ์ฉ๋๋ค. | |
| if (n === 1) return arr.map((v) => [v]); | |
| let result = []; | |
| // ์์๋ฅผ ์ํํ๋ค | |
| arr.forEach((fixed, idx, arr) => { | |
| // ํ์ฌ index๋ฅผ ์ ์ธํ ์์๋ฅผ ์ถ์ถํ๋ค. | |
| // index๋ฒ์งธ๋ ์ ํ๋ ์์ | |
| const rest = arr.filter((_, index) => index !== idx); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function combinations(arr, n) { | |
| // 1๊ฐ๋ง ๋ฝ๋๋ค๋ฉด ๊ทธ๋๋ก ์กฐํฉ์ ๋ฐํํ๋ค. ํ์ถ ์กฐ๊ฑด์ผ๋ก๋ ์ฌ์ฉ๋๋ค. | |
| if (n === 1) return arr.map((v) => [v]); | |
| const result = []; | |
| // ์์๋ฅผ ์ํํ๋ค | |
| arr.forEach((fixed, idx, arr) => { | |
| // ํ์ฌ index ์ดํ ์์๋ฅผ ์ถ์ถํ๋ค. | |
| // index๋ฒ์งธ๋ ์ ํ๋ ์์ | |
| const rest = arr.slice(idx + 1); |