본문 바로가기
728x90
반응형

JavaScript186

'this'를 갈고 닦자! 이번 글은 'JavaScriptの理解を深めた人がさらにもう1歩先に進むための本(JavaScript를 깊게 이해한 사람이 한 걸음 더 나아가기 위한 책)의 Chapter 3. thisを極めよう!(this를 갈고 닦자!)'를 바탕으로 작성하였습니다. 함수에서 호출했을 때의 'this' 함수에서 호출했을 때 this는 global object를 가리킨다. 대부분의 경우 JavaScript는 브라우저에서 동작하기 때문에 global object는 'window object'를 가리키는 것이다. function checkThis() { console.log(this); // Window ※ 브라우저에서 실행했을 경우 this.val = 'test'; // global scope member로 추가됨 } checkTh.. 2022. 4. 7.
하노이의 탑(Towers of Hanoi) References 이번 글은 아래 자료들을 참고하여 작성하였습니다. Towers of Hanoi on Khan Academy [파이썬]알고리즘 이야기(01. 하노이 탑) by 파이썬 클래쓰 on Youtube 무조건 이해시켜 드립니다. 비법 대방출! 재귀 알고리즘 Recursion - 하노이탑, 피보나치 수열 by 딩코딩 on Youtube 들어가며 하노이의 탑을 풀기 위해서는 우선 재귀 함수에 대한 이해가 필요하다. 재귀 함수는 javascript의 일반적인 동작 방식인 명령형(Imperative)이 아니라 선언형(declarative)으로 동작한다. 따라서 재귀 함수를 온전히 이해하려면 declarative programming에 대한 감각이 필요하다. 재귀 함수를 짤 때 중요한 것은 두 가지이다... 2022. 4. 7.
재귀 알고리즘(Recursive Algorithms) References 이번 글은 아래 자료들을 참고하여 작성하였습니다. Recursive Algorithms on Khan Academy Think Like a Programmer 위 인형은 러시아의 전통 인형 마트료시카이다. 큰 인형을 열면 그보다 작은 인형이 하나 나오고 다시 그 인형을 열면 또 작은 인형이 나오고를 반복하며 점점 크기가 작아진다. 재귀 함수도 이와 마찬가지로 주어진 조건을 충족할 때까지 함수 자신의 크기를 줄여가다 조건을 충족하면 결과값을 반환하는 함수이다. 배열 내 값 모두 더하기 // solution 1. const numArr1 = [1, 2, 3, 4, 5]; function iterativeArraySum(array, size) { let sum = 0; for (let i .. 2022. 4. 7.
퀵 정렬(Quick Sort) References 이번 글은 아래 자료를 참고하여 작성하였습니다. Insertion Sort on Khan Academy const partition = function (array, startIdx, lastIdx) { let partitonedArr = array, partitonedArrStartIdx = startIdx, partitonedArrLastIdx = lastIdx; const createPartitionedArr = function ( partitonedArr, partitonedArrStartIdx, partitonedArrLastIdx ) { let partitonedRightArrStartIdx = partitonedArr[partitonedArrStartIdx]; partit.. 2022. 4. 7.
삽입 정렬(Insertion Sort) References 이번 글은 아래 자료를 참고하여 작성하였습니다. Insertion Sort on Khan Academy Insertion Sort function insert(array, rightIndex, value) { // let temp; // for (let i = rightIndex; i >= 0; --i) { // if (value = 0 && array[i] > value; --i) { array[i + 1] = array[i]; array[i] = value; } } funct.. 2022. 4. 7.
합병 정렬(Merge Sort) References 이번 글은 아래 자료들을 참고하여 작성하였습니다. Section 33. Ack, MergeSort! Merge Sort on Khan Academy function mergeSort(arr) { if (arr.length === 1) { return arr; } const center = Math.floor(arr.length / 2); const left = arr.slice(0, center); const right = arr.slice(center); return merge(mergeSort(left), mergeSort(right)); } function merge(left, right) { const results = []; while (left.length && right... 2022. 4. 7.
선택 정렬(Selection Sort) References 이번 글은 아래 자료들을 참고하여 작성하였습니다. Section 32. Sort By Selection on Udemy Selection Sort on Khan Academy Selection Sort // solution 1. function selectionSort(arr) { for (let i = 0; i < arr.length; ++i) { let indexOfMin = i; // for문 j 돌면서 arr[j]가 arr[indexOfMin]보다 작으면 indexOfMin을 j로 치환한다. 즉, 현재값과 배열 내 값들을 비교하며, 현재 값보다 작은 값의 위치를 기록하는 것이다. for (let j = i + 1; j < arr.length; ++j) { if (arr[j] <.. 2022. 4. 7.
거품 정렬(Bubble Sort) References 아래 링크의 강의 중 Section 31. Sorting with BubbleSort의 내용을 추려 이번 글을 작성하였습니다. The Coding Interview Bootcamp: Algorithms + Data Structures on Udemy Bubble Sort function bubbleSort(arr) { for (let i = 0; i arr[j + 1]) { const lesser = arr[j + 1]; arr[j + 1] = arr[j]; arr[j] = lesser; } } } return arr; } Bubble Sor.. 2022. 4. 7.
이진 탐색 트리 유효성 검사(Validating a Binary Search Tree) References 아래 링크의 강의 중 Section 28. Validating a Binary Search Tree의 내용을 추려 이번 글을 작성하였습니다. The Coding Interview Bootcamp: Algorithms + Data Structures on Udemy Validating a Binary Search Tree function validate(node, min = null, max = null) { if (max !== null && node.data > max) { return false; } if (min !== null && node.data < min) { return false; } if (node.left && !validate(node.left, min, node.. 2022. 4. 7.
728x90
반응형