Eagle Black Ltd

Useful JS

Removing from an array as you iterate

The array is being re-indexed when you do a .splice(), which means you'll skip over an index when one is removed, and your cached .length is obsolete.

To fix it, you'd either need to decrement i after a .splice(), or simply iterate in reverse...

let i = Auction.auctions.length while (i--) { ... if (...) { Auction.auctions.splice(i, 1); } }

This way the re-indexing doesn't affect the next item in the iteration, since the indexing affects only the items from the current point to the end of the Array, and the next item in the iteration is lower than the current point.

Binary Search

function binarySearch(arr, val, start = 0, end = arr.length - 1) { const mid = Math.floor((start + end) / 2); if (val === arr[mid]) { return mid; } if (start >= end) { return -1; } return val < arr[mid] ? binarySearch(arr, val, start, mid - 1) : binarySearch(arr, val, mid + 1, end); }

Remove all node modules in a monorepo

npx rimraf node_modules \*\*/node_modules

Close any application using a port

npx kill-port 3009

Create simple http server

npx http-server