Skip to content

Instantly share code, notes, and snippets.

// *** Variables, Hoisting, Closures ***
// *Understanding of scope*
(function () {})();
(function () {
for (var vari = 5; vari > 0; vari--) {}
for (let leti = 5; leti > 0; --leti) {}
@Buggytheclown
Buggytheclown / computedProperties.js
Created September 15, 2019 09:17
a few way to get memoized value
class A extends Component<Props> {
sumMemoized = memoizeLast((a, b) => a + b);
computedAliasForOtherProp = memoizeLast(props => props.field.deep.nested);
get aliasForOtherProp() {
return this.computedAliasForOtherProp(props);
}
get result() {
return this.sumMemoized(this.props.a, this.aliasForOtherProp);
@Buggytheclown
Buggytheclown / FSM.js
Created June 23, 2019 17:01
Simple Automaton (Finite State Machine)
const q3 = () => q2;
const q2 = command => (command === '0' ? q3 : q2);
const q1 = command => (command === '1' ? q2 : q1);
function Automaton1() {
return {
readCommands(commands) {
const lastState = commands.reduce(
(state, command) => state(command),
q1
function lookLike(prop1, asProp2) {
if (prop1 === asProp2) {
return true;
}
if (typeof prop1 !== "object" || typeof asProp2 !== "object") {
return false;
}
return Object.entries(asProp2).every(([k, v]) => lookLike(prop1[k], v));
}
@Buggytheclown
Buggytheclown / bash-analytics.sh
Created January 3, 2019 14:53
bash analytics
# Find top 30 the most wanted files sorted by commit count
git log --name-only --pretty=format: src | sort | uniq -c | sort -nr | head -n 30
# Find top 10 the largest '*.js' files sorted by size
find . -name '*.js' -type f -ls | sort -k7 -r | head -n 20
# count lines of code, without directory 'migrations'
find . -name migrations -prune -o -name '*.ts' | xargs wc -l
@Buggytheclown
Buggytheclown / isPalOfPrimes.js
Last active September 15, 2018 06:36
Largest palindrome that is product of 2 prime 5 digit number
const MAX_DIVISOR = 99999;
const MIN_DIVISOR = 10000;
function isPalindrome(number) {
let rebmun = 0;
let remainder = number;
while (remainder !== 0) {
let lastDigit = remainder % 10;
rebmun = rebmun * 10 + lastDigit;
remainder = (remainder - lastDigit) / 10;
@Buggytheclown
Buggytheclown / prototype.js
Last active December 21, 2020 08:23
simplified version of JS inheritance
class CA {
static staticMethodA() {
return 42;
}
constructor(name = 'default name') {
this.name = name;
}
methodA() {
@Buggytheclown
Buggytheclown / tasks_JS.js
Last active January 28, 2020 12:23
Some good tasks for JS developers
/*
* Descriptions:
* Implement curry function.
*/
// Outputs:
function sum3(a, b, c){
return a + b + c;
}
var sum3_curryed = curry(sum3);
@Buggytheclown
Buggytheclown / fix_me_JS.js
Last active March 30, 2024 19:42
Basic javascript interview questions. (Some 'bad' working scripts)
/*
* IIFE
* https://en.wikipedia.org/wiki/Immediately-invoked_function_expression
*/
(function() {
})();
/*
* Comparison operators
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators