Skip to content

Instantly share code, notes, and snippets.

View lior-amsalem's full-sized avatar

Lior Amsalem lior-amsalem

View GitHub Profile
//localStorage.js
export const loadState = () => {
try {
const serializedState = localStorage.getItem('state');
if (serializedState === null) {
return undefined;
}
return JSON.parse(serializedState);
} catch (err) {
return undefined;
@lior-amsalem
lior-amsalem / custom-promise.js
Last active November 17, 2021 16:01
For educational purpose, here we have a custom made promise handler.
/**
This is for educational purpose only and for fun.
*/
class customPromise {
constructor(functionToResolve) {
this.status = "pending";
this.onFulfilledCallbacks = [];
this.onRejectedCallbacks = [];
@lior-amsalem
lior-amsalem / fibonacci-cached.js
Created November 12, 2021 06:54
codility cache fibonacci to improve performance
let cache = {};
var yourself = {
fibonacci : function(n) {
if (n === 0) {
return 0;
} else if (n === 1) {
return 1;
} else {
let r = cache[n] = cache[n] ? cache[n] : this.fibonacci(n - 1) +
@lior-amsalem
lior-amsalem / convert-to-camel-case.js
Created October 17, 2021 19:00
convert string to camelCase
/**
Examples
"the-stealth-warrior" gets converted to "theStealthWarrior"
"The_Stealth_Warrior" gets converted to "TheStealthWarrior"
**/
function toCamelCase(str){
return str.replace(/[-_][a-zA-Z]/g, (i) => i.replace(/[-_]/g,'').toUpperCase());
}
@lior-amsalem
lior-amsalem / roman-numerals-decoder.js
Created October 17, 2021 18:55
Roman Numerals Decoder
/**
solution('XXI'); // should return 21
**/
const helper = {
'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
@lior-amsalem
lior-amsalem / format-names.js
Created October 17, 2021 18:53
sort names with & and commas
/**
list([ {name: 'Bart'}, {name: 'Lisa'}, {name: 'Maggie'} ])
// returns 'Bart, Lisa & Maggie'
**/
function list(names){
let formatNames = '';
names.map((a,b) => (b < names.length-2) ? formatNames += a.name + ', ' : ((b === names.length-2) ? (formatNames += a.name + ' & ') : (formatNames += a.name)))
@lior-amsalem
lior-amsalem / snail-sort-array.js
Created October 17, 2021 18:50
Given an n x n array, return the array elements arranged from outermost elements to the middle element, traveling clockwise.
/**
array = [[1,2,3],
[4,5,6],
[7,8,9]]
snail(array) #=> [1,2,3,6,9,8,7,4,5]
**/
snail = function(array) {
let results = [],
botArray = [];
@lior-amsalem
lior-amsalem / strip-comments.js
Created October 17, 2021 18:49
Strip comment #test
/**
var result = solution("apples, pears # and bananas\ngrapes\nbananas !apples", ["#", "!"])
// result should == "apples, pears\ngrapes\nbananas"
**/
function solution(input, markers) {
return input.replace(new RegExp('(\\s?['+markers.join('?') + '?' + '\-?]\\s?)(.*)','gm'),'');
}
@lior-amsalem
lior-amsalem / create-breadcrumbs.js
Created October 17, 2021 18:43
Given url extract information from the URL and create a breadcrumbs. see example
/**
generateBC("mysite.com/pictures/holidays.html", " : ") == '<a href="/">HOME</a> : <a href="/pictures/">PICTURES</a> : <span class="active">HOLIDAYS</span>'
**/
function generateBC(url, separator) {
url = url.replace(/(http[s]?:\/\/[A-Za-z0-9]{3,}\.[A-Za-z]{2,})/g,'');
let r = '',
BC = ['/HOME'];
BC.push(...url.match(/\/(?!index)([A-Za-z0-9-]+)/gi)||[]);
@lior-amsalem
lior-amsalem / calculator.js
Created October 17, 2021 18:41
Create a simple calculator that given a string of operators (), +, -, *, / and numbers separated by spaces returns the value of that expression. see example below
/**
Example:
Calculator().evaluate("2 / 2 + 3 * 4 - 6") # => 7
**/
const Calculator = function() {
this.resolveMultiDivision = mathString => {
var resolvedTop = mathString.replace(new RegExp(/(\d*\.?\d*)([\/\*])(\d*\.?\d*)/), (match, group1,group2,group3) => {
return group2 === '*' ? group1 * group3 : group1 / group3;