Skip to content

Instantly share code, notes, and snippets.

View zlrlo's full-sized avatar
๐Ÿฅ

jieunee zlrlo

๐Ÿฅ
  • Seoul, Republic of Korea
View GitHub Profile
@zlrlo
zlrlo / makeCompany.js
Created November 21, 2022 01:25
javascript trick - ๊ฐ์ฒด ๋ณ‘ํ•ฉ
const makeCompany = (showAddress) => {
return {
name: 'Cobalt. Inc.',
...showAddress && { address: 'Seoul' }
}
};
@zlrlo
zlrlo / queue.js
Last active August 23, 2023 09:41
[JS] Queue javascript
// LinkedList
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
class Queue {
@zlrlo
zlrlo / padStart.js
Last active July 26, 2023 09:39
[JS] ๋ฌธ์ž์—ด์˜ ์‹œ์ž‘์„ ๋‹ค๋ฅธ ๋ฌธ์ž์—ด๋กœ ์ฑ„์šฐ๊ธฐ
const hour = '21';
const min = '53';
const sec = '5';
const time = `${hour}:${min}:${sec.padStart(2, '0')}`
console.log(time); // 21:53:05
@zlrlo
zlrlo / flatMap.js
Created July 26, 2023 09:39
[JS] ํ•œ ๋ ˆ๋ฒจ๋งŒ ํ‰ํƒ„ํ™” flatMap
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"]
@zlrlo
zlrlo / slice.js
Created July 26, 2023 09:52
[JS] slice ์–•์€ ๋ณต์‚ฌ
// 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)
@zlrlo
zlrlo / sort.js
Created July 26, 2023 09:58
[JS] ์ •๋ ฌ
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;
}
@zlrlo
zlrlo / max_heap.js
Last active July 29, 2023 03:06
[JS] Max Heap ๊ตฌํ˜„
class MaxHeap {
constructor() {
this.heap = [null];
}
push(value) {
this.heap.push(value);
let currentIndex = this.heap.length - 1;
let parentIndex = Math.floor(currentIndex / 2);
@zlrlo
zlrlo / getPrime.js
Last active October 3, 2023 02:26
[JS] ์†Œ์ˆ˜ ๊ตฌํ•˜๋Š” ๊ฐ€์žฅ ๋น ๋ฅธ ๋ฐฉ๋ฒ•
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;
}
@zlrlo
zlrlo / permutations.js
Created October 3, 2023 02:40
[JS] ์ˆœ์—ด ๊ตฌํ˜„
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);
@zlrlo
zlrlo / combinations.js
Created October 3, 2023 02:41
[JS] ์กฐํ•ฉ ๊ตฌํ˜„
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);