Skip to content

Instantly share code, notes, and snippets.

@mrharel
mrharel / solution.js
Last active January 14, 2019 18:30
Determine if any 3 integers in an array sum to 0. Note: The following solutions assumes that repetitions (i.e. choosing the same array element more than once) are allowed, so the array [-5,1,10] contains a zero sum (-5-5+10) and so does [0] (0+0+0). [4, 2, -1, 1, -5, 6, -4] = True
function detectZero(arr) {
const map = {};
for (var i=0; i<arr.length; i++) {
if (arr[i] === 0) return true;
if (map[arr[i]]) return true;
map[arr[i] * -2] = true;
if (find2Sum(arr.slice(i), -arr[i])) return true;
}
return false;
}
@mrharel
mrharel / proxyTrack.js
Created December 28, 2018 14:24
Using Proxy to Track Javascript Class
const callerMap = {};
function getCaller(error) {
if (error && error.stack) {
const lines = error.stack.split('\n');
if (lines.length > 2) {
let match = lines[2].match(/at ([a-zA-Z\-_$.]+) (.*)/);
if (match) {
return {
name: match[1].replace(/^Proxy\./, ''),
@mrharel
mrharel / palindrome.js
Created November 8, 2018 12:32
Palindromes in JavaScript
const word = "A man, a plan, a canal. Panama";
/*
* Checks if a string is a palindrome (as explained here: https://medium.freecodecamp.org/two-ways-to-check-for-palindromes-in-javascript-64fea8191fd7
* hard polindrome will not except string with white space.
*/
const palindrome = (w, hard = false) => {
const str = hard ? w : w.toLowerCase().replace(/[^a-zA-Z0-9]/g,"");
const left = str.substring(0, Math.floor(str.length/2));
@mrharel
mrharel / file-resize.js
Last active September 27, 2017 06:32
Resize and flip the file (using the exif lib) if needed
import EXIF from "exif-js";
const DEFAULT_MAX_SIZE = 1600;
const JPEG_QUALITY = 0.9;
const FILE_READER_STATE_DONE = 2;
const JPEG_FILETYPE = "image/jpeg";
/**
* Set up operations and degrees to rotate for each EXIF orientation (index).
*/
const ExifOrientations = [