TIL

RK_NFT_HOLDER·2022년 9월 28일
0

BEB_07_RK_NFT

목록 보기
2/10

제일 귀여운
재귀 함수다.
재미있었다.
메모리 접근 횟수 만큼
시간이 녹아 버리는 게 흠이지만.

약간의 정리 요약과
되새김을 위한 기록

Array.prototype.slice()

The slice() method returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included) where start and end represent the index of items in that array. The original array will not be modified.

Try it

JavaScript Demo: Array.slice()

const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];

console.log(animals.slice(2));
// expected output: Array ["camel", "duck", "elephant"]

console.log(animals.slice(2, 4));
// expected output: Array ["camel", "duck"]

console.log(animals.slice(1, 5));
// expected output: Array ["bison", "camel", "duck", "elephant"]

console.log(animals.slice(-2));
// expected output: Array ["duck", "elephant"]

console.log(animals.slice(2, -1));
// expected output: Array ["camel", "duck"]

console.log(animals.slice());
// expected output: Array ["ant", "bison", "camel", "duck", "elephant"]

Array.prototype.concat()

The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.

Try it

JavaScript Demo: Array.concat()


const array1 = ['a', 'b', 'c'];
const array2 = ['d', 'e', 'f'];
const array3 = array1.concat(array2);

console.log(array3);
// expected output: Array ["a", "b", "c", "d", "e", "f"]

function take(num, arr) {

if (arr.length <= num ){
return arr
}else if(arr.length ===0 || num === 0){
return []
}
const head = arr[0]
const tail = arr.slice(1)
return [head].concat(take(num - 1, tail));
}

function drop(num, arr) {

if (num === 0 || arr.length === 0){
return arr
}
const tail = arr.slice(1)
return drop(num-1,tail)
}

function arrLength(arr) {

if (arr.isEmpty()){
return 0
}
const tail = arr.slice(1)
return 1 + arrLength(tail)
}

function reverseArr(arr) {

if(arr.length === 0){
return []
}
const head = arr[0];
const tail = arr.slice(1);
return reverseArr(tail).concat(head)
}

function findMatryoshka(matryoshka, size) {

  if(matryoshka.size === size )    {
    return true
    }else if(matryoshka.matryoshka && matryoshka.size > size){
    return findMatryoshka(matryoshka.matryoshka, size)
  }

  return false

}

function unpackGiftbox(giftBox, wish) {

// recursive case
for (let i = 0; i < giftBox.length; i++) {
if (giftBox[i] === wish) {
return true;
} else if (Array.isArray(giftBox[i])) {
const result = unpackGiftbox(giftBox[i], wish);
if (result === true) {
return true;
}
}
}

// base case
return false;
}
느어무 안풀리던 문제
이건 풀이과정 다 외워버려야지

function unpackGiftbox(giftBox, wish) {

// recursive case
let anotherBoxes = [];
for (let i = 0; i < giftBox.length; i++) {
if (giftBox[i] === wish) {
return true;
} else if (Array.isArray(giftBox[i])) {
anotherBoxes = anotherBoxes.concat(giftBox[i]);
}
}

if (anotherBoxes.length > 0) {
return unpackGiftbox(anotherBoxes, wish);
}

// base case
return false;
}

function unpackGiftbox(giftBox, wish) {

const result = giftBox.reduce((acc, curr) => {
if (curr === wish) {
return true;
} else if (Array.isArray(curr)) {
return unpackGiftbox(curr, wish) || acc;
} else {
return acc;
}
}, false);
return result;
}

function flattenArr(arr) {

// base case
if (arr.length === 0) {
return [];
}

// recursive case
const head = arr[0];
const tail = arr.slice(1);
if (Array.isArray(head)) {
return flattenArr([...head, ...tail]);
} else {
return [head].concat(flattenArr(tail));
}
}
의뢰로 쉽게 풀린 녀석

function flattenArr(arr) {

const result = [];
for (let i = 0; i < arr.length; i++) {
if (Array.isArray(arr[i])) {
const flattend = flattenArr(arr[i]);
result.push(...flattend);
} else {
result.push(arr[i]);
}
}
return result;
}

function flattenArr(arr) {

return arr.reduce(function (result, item) {
if (Array.isArray(item)) {
const flattened = flattenArr(item);
return [...result, ...flattened];
} else {
return [...result, item];
}
}, []);
}

function findMatryoshka(matryoshka, size) {

  if(matryoshka.size === size )    {
    return true
    }else if(matryoshka.matryoshka && matryoshka.size > size){
    return findMatryoshka(matryoshka.matryoshka, size)
  }

  return false

}

[A].concat(B)
A엘리먼트가 있는 배열에 B배열 엘리먼트를 몽땅 넣어버려라

[...A, ...B]
A것들 B것들 다 헤쳐모여

arr.slice(1)
arr 첫 번째 엘리먼트를 잘라내고 배열 보여주라

많이 풀고 많이 보면 로직 이해되기 마련
코드 즐감 해랍

profile
blockchain

6개의 댓글

comment-user-thumbnail
2022년 9월 28일

내일도 졸귀여운 재귀함수
신난다
https://www.youtube.com/watch?v=-5KAN9_CzSA

1개의 답글
comment-user-thumbnail
2022년 9월 28일

14번 ㅂㄷㅂㄷ.... 저희는 14번 디버깅에 한시간 썼습니다...

2개의 답글