Skip to main content

Longest Palindromic Substring

0 likes • Nov 19, 2022 • 0 views
JavaScript
Loading...

More JavaScript Posts

function printHeap(heap, index, level) {
if (index >= heap.length) {
return;
}
console.log(" ".repeat(level) + heap[index]);
printHeap(heap, 2 * index + 1, level + 1);
printHeap(heap, 2 * index + 2, level + 1);
}
//You can call this function by passing in the heap array and the index of the root node, which is typically 0, and level = 0.
let heap = [3, 8, 7, 15, 17, 30, 35, 2, 4, 5, 9];
printHeap(heap,0,0)

LeetCode Product Except Self

CHS
0 likes • Sep 13, 2023 • 6 views
JavaScript
/**
* @param {number[]} nums
* @return {number[]}
*/
var productExceptSelf = function(nums) {
var output = [];
var leftMult = 1;
var rightMult = 1;
for (var i=nums.length - 1; i >= 0; i--) {
output[i] = rightMult;
rightMult *= nums[i];
console.log({output: JSON.stringify(output), i})
}
for (var j=0; j < nums.length; j++) {
output[j] *= leftMult;
leftMult *= nums[j];
console.log({output: JSON.stringify(output), j})
}
return output;
};
console.log(productExceptSelf([1, 2, 3, 4]))

unzipWith() function

0 likes • Nov 19, 2022 • 0 views
JavaScript
const unzipWith = (arr, fn) =>
arr
.reduce(
(acc, val) => (val.forEach((v, i) => acc[i].push(v)), acc),
Array.from({
length: Math.max(...arr.map(x => x.length))
}).map(x => [])
)
.map(val => fn(...val));
unzipWith(
[
[1, 10, 100],
[2, 20, 200],
],
(...args) => args.reduce((acc, v) => acc + v, 0)
);
// [3, 30, 300]

Sequential Queue

0 likes • Feb 6, 2021 • 2 views
JavaScript
class SequentialQueue{ //if you want it to go backwards, too bad
next(){
return this.i++;
}
constructor(start = 0){
this.i = start;
}
}
const que = new SequentialQueue(0);
for(let i = 0; i < 10; i++){
console.log(que.next());
}

Generate a random HEX color

0 likes • Nov 18, 2022 • 0 views
JavaScript
// Generate a random HEX color
randomColor = () => `#${Math.random().toString(16).slice(2, 8).padStart(6, '0')}`;
// Or
const randomColor = () => `#${(~~(Math.random()*(1<<24))).toString(16)}`;

JS Test

0 likes • Mar 9, 2021 • 1 view
JavaScript
alert("bruh")