Skip to content

Instantly share code, notes, and snippets.

View karolk's full-sized avatar
💭
cooking on gas

Karol K karolk

💭
cooking on gas
View GitHub Profile
@karolk
karolk / falling-letters.html
Last active June 15, 2022 12:57
Very simple html and JS game created as an assignment to a 14 yo aspiring programmer!
<!doctype html>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
//JS to explain
//literals var function = + a.b ()
//methods to explain
@karolk
karolk / testing-tpt.md
Last active January 26, 2022 19:41
Testing TPT

Testing TPT

Electron 24/01/2022

LOGIN

  • login free
  • login premium
  • remember password
  • not remember password
@karolk
karolk / await-vs-then.md
Created September 28, 2020 20:03
await vs. then

I recently run into a scenario highlighting that await is not always a drop-in, better replacement for then. Sometimes with then it's easier to control the scope of the function that runs after the promise is settled. I have a Redux middleware-inspired example here:

https://codesandbox.io/s/eager-wildflower-1kodv?file=/src/index.js

In example 2, the next call will only happen after the promise resolves.

In example 3, the next will be called immediately.

@karolk
karolk / have-same-keys1.js
Last active June 23, 2020 15:36
Do 2 objects have the same keys - 2 ways
const haveSameKeys = (objA, objB) => {
const keysOfA = Object.keys(objA)
const keysOfB = Object.keys(objB)
return keysOfA.length === keysOfB.length &&
keysOfA.every(key => keysOfB.includes(key))
}
@karolk
karolk / letter.js
Created April 25, 2020 21:27
vanilla js implementation of letters
// @ts-check
const createRandomInteger = (limit) => Math.floor(Math.random() * limit)
const createRandomLetter = () => String.fromCharCode(97 + createRandomInteger(26));
const positionLetter = (element) => {
element.style.width = '1vw'
element.style.height = '1vw'
element.style.lineHeight = '1vw'
@karolk
karolk / multiple inheritance.md
Created October 5, 2012 09:41
Comparison of a multiple inheritance implementation in ES3 and ES5

I am always baffled by the complicated graphs people draw to explain the ES3 concept for inheritance. I don't think inheritance is the right word for what is in the language. The more appropriate word would be composition. Prototypal composition, because an object called prototype is used to bring in that magic. If you think this means JS doesn't have inheritance, don't forget that the goal is not to be able to inherit. What we are trying to achieve is code reuse, small memory footprint and polymorhism (defined here as an ability to mutate object slightly in relation to their generic prototypes).

ES3 prototypal composition pattern

ES3 in it's attempt to imitate Java (for familiarity purposes) emphasised the role of the class in instances creation. The idea was/is as follows

//1. Functions also serve as classes. There is no separate `class` keyword.

function Animal( sound ) {
@karolk
karolk / espot-track.js
Last active April 26, 2019 08:54
Click and impression tracking code for espots
if (typeof handleTrackableClickEvent !== "function") {
var handleTrackableClickEvent = function(event) {
var trackableSelector = '.trackable';
if (event.target) {
// msMatches is for IE
var matches = event.target.matches || event.target.msMatches;
if (matches && matches.call(event.target, trackableSelector)) {
var et = event.target;
@karolk
karolk / primes.js
Created March 8, 2019 17:27
Finding primes brute force in finite time
const isPrime = number => {
let divisor = 2
while (divisor < number) {
if(number % divisor === 0) return false
divisor += 1
}
return true
}
const findPrime = (timeLimitMS) => {
@karolk
karolk / proto-version.js
Last active June 7, 2018 10:34
versions in prototype chain
// IDEA: start with an object using a symbol as a marker
const create = (obj, changeSet) => Object.assign(Object.create(obj || {[Symbol.for('versionstart')]: true}), changeSet);
const getPreviousState = obj => {
const maybePrev = Object.getPrototypeOf(obj);
if (!maybePrev || maybePrev[Symbol.for('versionstart')]) {
return null;
}
return maybePrev;
}
@karolk
karolk / graph-by.js
Last active June 6, 2018 13:02
Graph data by sequential prop
// Given a list with data containing sequential property such as year of birth, age, size
// Create a graph containing elements groupped by that property with ids of next nodes
// Output example
{
'1': {list: [{size: 1}, {size: 1}], previous: undefined, next: '3'},
'3': {list: [{size: 3}], previous: '1', next: '4'},
'4': {list: [{size: 4}], previous: '3', next: undefined},
}