λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
μΉ΄ν…Œκ³ λ¦¬ μ—†μŒ

μžλ°”μŠ€ν¬λ¦½νŠΈλ‘œ λ°°μ—΄μ˜ Push,Pop,Sort, Reverseκ΅¬ν˜„ν•˜κΈ°

by 5566 2023. 10. 25.

1. Push λ©”μ„œλ“œ κ΅¬ν˜„ν•˜κΈ°

κ°œμš”

Push λ©”μ„œλ“œλŠ” λ°°μ—΄μ˜ 끝에 ν•˜λ‚˜ μ΄μƒμ˜ μš”μ†Œλ₯Ό μΆ”κ°€ν•˜λŠ” μš©λ„λ‘œ μ‚¬μš©λ©λ‹ˆλ‹€. 이번 μž₯μ—μ„œλŠ” μžλ°”μŠ€ν¬λ¦½νŠΈμ˜ κΈ°λ³Έ λ°°μ—΄ λ©”μ„œλ“œμΈ Pushλ₯Ό 직접 κ΅¬ν˜„ν•΄λ³΄λ„λ‘ ν•˜κ² μŠ΅λ‹ˆλ‹€.

μ•Œκ³ λ¦¬μ¦˜ λ™μž‘

  1. μž…λ ₯된 μš”μ†Œλ₯Ό λ°°μ—΄μ˜ λ§ˆμ§€λ§‰ 인덱슀 λ‹€μŒ μœ„μΉ˜μ— μΆ”κ°€ν•©λ‹ˆλ‹€.
  2. λ°°μ—΄μ˜ 길이λ₯Ό 1 μ¦κ°€μ‹œν‚΅λ‹ˆλ‹€.
  3. μƒˆλ‘œμš΄ 길이λ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.

κ΅¬ν˜„

λ‹€μŒμ€ Push λ©”μ„œλ“œμ˜ κ΅¬ν˜„ μ˜ˆμ‹œμž…λ‹ˆλ‹€.

Array.prototype.pushCustom = function (...elements) {
  const startLength = this.length;
  let argIndex = 0;

  for (let i = startLength; i < startLength + elements.length; i++) {
    this[i] = elements[argIndex++];
  }

  return this.length;
};

μ‚¬μš©λ²•

λ‹€μŒμ€ pushCustom λ©”μ„œλ“œμ˜ μ‚¬μš© μ˜ˆμ‹œμž…λ‹ˆλ‹€.

const fruits = ['apple', 'banana'];

console.log(fruits.pushCustom('orange')); // 3
console.log(fruits); // ['apple', 'banana', 'orange']

console.log(fruits.pushCustom('grape', 'kiwi')); // 5
console.log(fruits); // ['apple', 'banana', 'orange', 'grape', 'kiwi']

μš”μ•½

Push λ©”μ„œλ“œλŠ” λ°°μ—΄ 끝에 μš”μ†Œλ₯Ό μΆ”κ°€ν•˜λŠ” λ©”μ„œλ“œλ‘œ, μœ„ μ˜ˆμ‹œλ₯Ό μ°Έκ³ ν•˜μ—¬ 직접 κ΅¬ν˜„ν•  수 μžˆμŠ΅λ‹ˆλ‹€. PushCustom λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•˜λ©΄ κΈ°μ‘΄ 배열에 μš”μ†Œλ₯Ό μ‰½κ²Œ μΆ”κ°€ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

2. Pop λ©”μ„œλ“œ κ΅¬ν˜„ν•˜κΈ°

κ°œμš”

Pop λ©”μ„œλ“œλŠ” λ°°μ—΄μ˜ λ§ˆμ§€λ§‰ μš”μ†Œλ₯Ό μ œκ±°ν•˜κ³  λ°˜ν™˜ν•©λ‹ˆλ‹€. 이번 μž₯μ—μ„œλŠ” μžλ°”μŠ€ν¬λ¦½νŠΈμ˜ κΈ°λ³Έ λ°°μ—΄ λ©”μ„œλ“œμΈ Pop을 직접 κ΅¬ν˜„ν•΄λ³΄λ„λ‘ ν•˜κ² μŠ΅λ‹ˆλ‹€.

μ•Œκ³ λ¦¬μ¦˜ λ™μž‘

  1. λ°°μ—΄μ˜ λ§ˆμ§€λ§‰ μš”μ†Œλ₯Ό μ œκ±°ν•©λ‹ˆλ‹€.
  2. λ°°μ—΄μ˜ 길이λ₯Ό 1 κ°μ†Œμ‹œν‚΅λ‹ˆλ‹€.
  3. 제거된 μš”μ†Œλ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.

κ΅¬ν˜„

λ‹€μŒμ€ Pop λ©”μ„œλ“œμ˜ κ΅¬ν˜„ μ˜ˆμ‹œμž…λ‹ˆλ‹€.

Array.prototype.popCustom = function () {
  if (this.length === 0) return;

  const removedElement = this[this.length - 1];
  delete this[this.length - 1];
  this.length--;

  return removedElement;
};

μ‚¬μš©λ²•

λ‹€μŒμ€ popCustom λ©”μ„œλ“œμ˜ μ‚¬μš© μ˜ˆμ‹œμž…λ‹ˆλ‹€.

const fruits = ['apple', 'banana', 'orange'];

console.log(fruits.popCustom()); // 'orange'
console.log(fruits); // ['apple', 'banana']

console.log(fruits.popCustom()); // 'banana'
console.log(fruits); // ['apple']

μš”μ•½

Pop λ©”μ„œλ“œλŠ” λ°°μ—΄μ˜ λ§ˆμ§€λ§‰ μš”μ†Œλ₯Ό μ œκ±°ν•˜κ³  λ°˜ν™˜ν•˜λŠ” λ©”μ„œλ“œμž…λ‹ˆλ‹€. μœ„ μ˜ˆμ‹œλ₯Ό μ°Έκ³ ν•˜μ—¬ 직접 PopCustom λ©”μ„œλ“œλ₯Ό κ΅¬ν˜„ν•˜λ©΄ λ°°μ—΄μ˜ λ§ˆμ§€λ§‰ μš”μ†Œλ₯Ό μ‰½κ²Œ μ œκ±°ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

3. Sort λ©”μ„œλ“œ κ΅¬ν˜„ν•˜κΈ°

κ°œμš”

Sort λ©”μ„œλ“œλŠ” λ°°μ—΄μ˜ μš”μ†Œλ₯Ό μ •λ ¬ν•©λ‹ˆλ‹€. 이번 μž₯μ—μ„œλŠ” μžλ°”μŠ€ν¬λ¦½νŠΈμ˜ κΈ°λ³Έ λ°°μ—΄ λ©”μ„œλ“œμΈ Sortλ₯Ό 직접 κ΅¬ν˜„ν•΄λ³΄λ„λ‘ ν•˜κ² μŠ΅λ‹ˆλ‹€.

μ•Œκ³ λ¦¬μ¦˜ λ™μž‘

  1. λ°°μ—΄μ˜ 첫 번째 μš”μ†ŒλΆ€ν„° λ§ˆμ§€λ§‰ μš”μ†ŒκΉŒμ§€ λ°˜λ³΅ν•˜λ©΄μ„œ μΈμ ‘ν•œ μš”μ†ŒλΌλ¦¬ λΉ„κ΅ν•©λ‹ˆλ‹€.
  2. 비ꡐ μ‹œ, 두 μš”μ†Œμ˜ 값을 λ¬Έμžμ—΄λ‘œ λ°”κΎΌ λ’€ Unicode μˆœμ„œλ‘œ λΉ„κ΅ν•©λ‹ˆλ‹€.
  3. μ •λ ¬ μˆœμ„œμ— λ§žμ§€ μ•Šμ€ 경우, μš”μ†Œλ₯Ό κ΅ν™˜ν•©λ‹ˆλ‹€.
  4. λͺ¨λ“  μš”μ†Œμ— λŒ€ν•΄ λ°˜λ³΅ν•˜λ©° 정렬을 μ™„λ£Œν•©λ‹ˆλ‹€.

κ΅¬ν˜„

λ‹€μŒμ€ Sort λ©”μ„œλ“œμ˜ κ΅¬ν˜„ μ˜ˆμ‹œμž…λ‹ˆλ‹€.

Array.prototype.sortCustom = function () {
  for (let i = 0; i < this.length - 1; i++) {
    for (let j = i + 1; j < this.length; j++) {
      if (String(this[j]) < String(this[i])) {
        [this[i], this[j]] = [this[j], this[i]];
      }
    }
  }

  return this;
};

μ‚¬μš©λ²•

λ‹€μŒμ€ sortCustom λ©”μ„œλ“œμ˜ μ‚¬μš© μ˜ˆμ‹œμž…λ‹ˆλ‹€.

const numbers = [5, 1, 3, 2, 4];

console.log(numbers.sortCustom()); // [1, 2, 3, 4, 5]

const fruits = ['apple', 'banana', 'orange'];

console.log(fruits.sortCustom()); // ['apple', 'banana', 'orange']

μš”μ•½

Sort λ©”μ„œλ“œλŠ” 배열을 μ •λ ¬ν•˜λŠ” λ©”μ„œλ“œλ‘œ, μœ„ μ˜ˆμ‹œλ₯Ό μ°Έκ³ ν•˜μ—¬ 직접 SortCustom λ©”μ„œλ“œλ₯Ό κ΅¬ν˜„ν•  수 μžˆμŠ΅λ‹ˆλ‹€. SortCustom λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•˜λ©΄ λ°°μ—΄μ˜ μš”μ†Œλ₯Ό μ‰½κ²Œ μ •λ ¬ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

4. Reverse λ©”μ„œλ“œ κ΅¬ν˜„ν•˜κΈ°

κ°œμš”

Reverse λ©”μ„œλ“œλŠ” λ°°μ—΄μ˜ μš”μ†Œλ“€μ„ λ°˜μ „μ‹œν‚΅λ‹ˆλ‹€. 이번 μž₯μ—μ„œλŠ” μžλ°”μŠ€ν¬λ¦½νŠΈμ˜ κΈ°λ³Έ λ°°μ—΄ λ©”μ„œλ“œμΈ Reverseλ₯Ό 직접 κ΅¬ν˜„ν•΄λ³΄λ„λ‘ ν•˜κ² μŠ΅λ‹ˆλ‹€.

μ•Œκ³ λ¦¬μ¦˜ λ™μž‘

  1. λ°°μ—΄μ˜ 첫 번째 μš”μ†Œμ™€ λ§ˆμ§€λ§‰ μš”μ†Œλ₯Ό λ°”κΏ‰λ‹ˆλ‹€.
  2. 두 번째 μš”μ†Œμ™€ λμ—μ„œ 두 번째 μš”μ†Œλ₯Ό λ°”κΏ‰λ‹ˆλ‹€.
  3. μœ„ 과정을 λ°°μ—΄μ˜ μ€‘κ°„κΉŒμ§€ λ°˜λ³΅ν•©λ‹ˆλ‹€.

κ΅¬ν˜„

λ‹€μŒμ€ Reverse λ©”μ„œλ“œμ˜ κ΅¬ν˜„ μ˜ˆμ‹œμž…λ‹ˆλ‹€.

Array.prototype.reverseCustom = function () {
  let i = 0;
  let j = this.length - 1;

  while (i < j) {
    [this[i], this[j]] = [this[j], this[i]];
    i++;
    j--;
  }

  return this;
};

μ‚¬μš©λ²•

λ‹€μŒμ€ reverseCustom λ©”μ„œλ“œμ˜ μ‚¬μš© μ˜ˆμ‹œμž…λ‹ˆλ‹€.

const numbers = [1, 2, 3, 4, 5];

console.log(numbers.reverseCustom()); // [5, 4, 3, 2, 1]

const fruits = ['apple', 'banana', 'orange'];

console.log(fruits.reverseCustom()); // ['orange', 'banana', 'apple']

μš”μ•½

Reverse λ©”μ„œλ“œλŠ” λ°°μ—΄μ˜ μš”μ†Œλ“€μ„ λ°˜μ „μ‹œν‚€λŠ” λ©”μ„œλ“œμž…λ‹ˆλ‹€. μœ„ μ˜ˆμ‹œλ₯Ό μ°Έκ³ ν•˜μ—¬ 직접 ReverseCustom λ©”μ„œλ“œλ₯Ό κ΅¬ν˜„ν•˜λ©΄ λ°°μ—΄μ˜ μš”μ†Œλ₯Ό κ°„λ‹¨νžˆ λ°˜μ „μ‹œν‚¬ 수 μžˆμŠ΅λ‹ˆλ‹€.

5. 마무리

이번 κΈ€μ—μ„œλŠ” μžλ°”μŠ€ν¬λ¦½νŠΈμ˜ κΈ°λ³Έ λ°°μ—΄ λ©”μ„œλ“œ 쀑 push, pop, shift, unshift, slice, splice, indexOf, includes, filter, map, reduce, forEach, some, every, sort, reverse λ©”μ„œλ“œλ₯Ό 직접 κ΅¬ν˜„ν•΄λ³΄λŠ” 방법에 λŒ€ν•΄ μ•Œμ•„λ³΄μ•˜μŠ΅λ‹ˆλ‹€.

각 λ©”μ„œλ“œλŠ” 배열을 λ‹€λ£¨λŠ” λ‹€μ–‘ν•œ κΈ°λŠ₯을 μ œκ³΅ν•˜λ©°, 이λ₯Ό κ΅¬ν˜„ν•˜κ³  μ‚¬μš©ν•¨μœΌλ‘œμ¨ 배열을 보닀 μœ μ—°ν•˜κ²Œ λ‹€λ£° 수 μžˆμŠ΅λ‹ˆλ‹€. 각 λ©”μ„œλ“œμ˜ λ™μž‘κ³Ό μ•Œκ³ λ¦¬μ¦˜μ— λŒ€ν•΄ μžμ„Ένžˆ μ•Œμ•„λ³΄κ³ , 예제 μ½”λ“œλ₯Ό 톡해 μ‹€μ œ μ‚¬μš©λ²•μ„ μ΅ν˜”μŠ΅λ‹ˆλ‹€.

μ΄λŸ¬ν•œ λ°°μ—΄ λ©”μ„œλ“œλ“€μ€ μžλ°”μŠ€ν¬λ¦½νŠΈμ—μ„œ ν”νžˆ μ‚¬μš©λ˜λŠ” κΈ°λŠ₯λ“€μ΄λ―€λ‘œ, λ©”μ„œλ“œλ“€μ˜ λ™μž‘ 원리λ₯Ό μ΄ν•΄ν•˜κ³  직접 κ΅¬ν˜„ν•΄λ³΄λŠ” 것은 ν”„λ‘œκ·Έλž˜λ° μ‹€λ ₯ ν–₯상에 도움이 될 κ²ƒμž…λ‹ˆλ‹€. λ˜ν•œ 직접 κ΅¬ν˜„ν•¨μœΌλ‘œμ¨ μžμ‹ λ§Œμ˜ μ»€μŠ€ν…€ λ©”μ„œλ“œλ₯Ό λ§Œλ“€μ–΄ μ‚¬μš©ν•  μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€.

μ•žμœΌλ‘œλ„ μžλ°”μŠ€ν¬λ¦½νŠΈμ˜ λ‹€μ–‘ν•œ κΈ°λŠ₯듀을 νƒκ΅¬ν•˜κ³ , 이λ₯Ό μ‹€μ œ μ½”λ“œμ— μ μš©ν•˜μ—¬ ν”„λ‘œκ·Έλž˜λ° μ‹€λ ₯을 ν–₯μƒμ‹œμΌœ λ‚˜κ°€λŠ” 것을 μΆ”μ²œν•©λ‹ˆλ‹€.

λŒ“κΈ€