Skip to content

Instantly share code, notes, and snippets.

View UniBreakfast's full-sized avatar
💡
reinventing basic stuff in order to understand it better

Mykhailo "Ninin" Velykoselskyi UniBreakfast

💡
reinventing basic stuff in order to understand it better
View GitHub Profile
proveWithStats(100000)
function proveWithStats(steps) {
const winStats = {change: 0, keep: 0}
for (let i = 0; i < steps/2; i++) {
playRound('keep', winStats)
playRound('change', winStats)
}
@UniBreakfast
UniBreakfast / watch-file.js
Created November 25, 2023 21:44
A node.js function `watchFile(filePath, contentCallback)` that can handle path like `folder/file.ext` and is ready to folder and/or file not be there at any given point in time.
const { existsSync, watch, readFile } = require('fs');
const { dirname, basename } = require('path');
function watchFile(filePath, contentCallback) {
const folderPath = dirname(filePath);
const folderName = basename(folderPath);
const parentPath = dirname(folderPath);
const fileName = basename(filePath);
let folderExists = existsSync(folderPath);
@UniBreakfast
UniBreakfast / build-matrix.js
Created October 17, 2023 14:47
A function to build two-dimensional array of sequential numbers
buildMatrix(3, 4, 5, 'right-up')
// [
// [1, 2, 3, 4],
// [2, 3, 4, 5],
// [3, 4, 5, 6],
// ]
function buildMatrix0(rCount, cCount, countStart, direction) {
const [first, then] = direction.split('-')
const matrix = Array(rCount).fill().map(() => [])
@UniBreakfast
UniBreakfast / giveCoins.js
Created May 19, 2023 19:45
Minimum required coins to reach the given input amount
/*
Task: Minimum required coins to reach the given input amount
Given a list of coins: 500, 200, 100, 50, 20, 10, 2, 1, 0.5, 0.2, 0.1, 0.02, 0.01
Given input amount: 3763.58
Expected output:
{
500:7,
200:1,
@UniBreakfast
UniBreakfast / logDecorator.js
Created January 29, 2023 12:30
logDecorator makes decorated function that logs to the console function name, arguments and result.
sub(mult(2, 5), add(2, div(12, 3))) // 4
function sub(x, y){
return x - y
}
function mult(x, y){
return x * y
}
String.prototype.split_ = function (sep, limit = this.length) {
if (sep === undefined) return [String(this)]
if (limit === Infinity) return []
const parts = [], length = this.length, len = sep.length
const isSepStart = i => {
for (let j = 0; j < len; j++) {
if (this[i + j] != sep[j]) return false
}
function decToBin(int) {
let bin = ''
while (int) {
const digit = int % 2
bin = digit + bin
int -= digit
int /= 2
}
@UniBreakfast
UniBreakfast / getterSetter.js
Created October 14, 2021 16:47
Getters and setter on rectangle and circle objects
const rectangle = {
a: 4,
b: 5,
get perimeter() {
return this.a * 2 + this.b * 2;
},
get area() {
return this.a * this.b;
},
};
@UniBreakfast
UniBreakfast / bagOfRandom.js
Created October 3, 2021 08:41
Handmade iterable object applying iterator protocol
const bagOfRandom = {
[Symbol.iterator]: () => ({
next: () => {
if (Math.random() <= 0.01) return { done: true }
return { value: Math.floor(Math.random() * 100 + 1), done: false }
}
})
}
@UniBreakfast
UniBreakfast / mapThisArg.js
Created October 1, 2021 08:32
Higher order methods with thisArg usage
prices = [10, 25, 70, 100, 120]
discountFuncs = [5, 10, 25, 30]
.map(num => x => '$' + (x / 100 * (100-num)).toFixed(2))
discountFuncs.map([].map, prices)
// (4) [Array(5), Array(5), Array(5), Array(5)]
// 0: (5) ['$9.50', '$23.75', '$66.50', '$95.00', '$114.00']
// 1: (5) ['$9.00', '$22.50', '$63.00', '$90.00', '$108.00']
// 2: (5) ['$7.50', '$18.75', '$52.50', '$75.00', '$90.00']
// 3: (5) ['$7.00', '$17.50', '$49.00', '$70.00', '$84.00']