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
| // Recursive implementation | |
| function heapify(arr, itemIndex, length) { | |
| // we add 1 because we start from 0 index | |
| let left = 2 * itemIndex + 1; | |
| let right = left + 1; | |
| let largest = itemIndex; | |
| if (left < length && arr[left] >= arr[largest]) { | |
| largest = left; | |
| } | |
| if (left < length && arr[right] >= arr[largest]) { |
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 mergeSort(arr) { | |
| if (arr.length <= 1) { | |
| return arr | |
| } | |
| const middlePoint = Math.floor(arr.length / 2); | |
| const rightArray = arr.slice(middlePoint); | |
| const leftArray = arr.slice(0, middlePoint); | |
| return merge(mergeSort(leftArray), mergeSort(rightArray)); |
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 arr = [4, 9, 10, 3, 7]; | |
| const sort = (arr) => { | |
| // starting from the 2nd index | |
| let i = 1; | |
| // loop through the array | |
| while (i < arr.length) { | |
| // loop back to make sure previous items are sorted (previous elements are less than the current value), | |
| // if previous element is not sorted(less) we switch | |
| while (arr[i] < arr[i-1]) { |
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 getFirstUnique(arr) { | |
| return arr.find(e => arr.indexOf(e) === arr.lastIndexOf(e)); | |
| } | |
| console.log(getFirstUnique([3, 1, 2, 3, 3, 3])); |
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
| /* | |
| Write a JavaScript program to get the integers in range (x, y). | |
| Example : range(2, 9) | |
| Expected Output : [3, 4, 5, 6, 7, 8] | |
| */ | |
| function getBetween(x, y) { | |
| if (x == y) { | |
| return [x] | |
| } else { |
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 factorial(n) { | |
| if (n === 1) { | |
| return 1; | |
| } | |
| return n * factorial(n - 1) | |
| } | |
| console.log(factorial(4)); // 24 |
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
| //Old task in node.js aimed for scheduling sessions for trainees in a gym subscription system | |
| // TODO create schedule for number of sessions | |
| // giving right dates and days for the new sessions schedule | |
| // distributed on number of sessions per week with specific weekdays | |
| // starting from specific date | |
| const moment = require('moment'); | |
| // TODO use moment weekday method | |
| var weekday = new Array(7); |
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 getUniqueElementsinArray(arr) { | |
| // Check if the given value is the first occurring. If not, it must be a duplicate and will be filtered | |
| return arr.filter((e, index) => arr.indexOf(e) === index); | |
| } | |
| console.log(getUniqueElementsinArray([1, 1, 2, 3, 3, 3, 3])); // [1, 2, 3] | |
| function getUniqueElementsUsingSet(arr) { | |
| // Set only accepts unique values | |
| return Array.from(new Set(arr)); |
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* fibonacci () { | |
| let [prev, curr] = [0, 1]; | |
| for (;;) { | |
| [prev, curr] = [curr, prev + curr]; | |
| yield curr; | |
| } | |
| } | |
| for (n of fibonacci()) { | |
| // truncate the sequence at 1000 |
NewerOlder