Skip to content

Instantly share code, notes, and snippets.

@codejockie
codejockie / codility_solutions.txt
Created July 27, 2020 04:50 — forked from lalkmim/codility_solutions.txt
Codility Solutions in JavaScript
Lesson 1 - Iterations
- BinaryGap - https://codility.com/demo/results/trainingU2FQPQ-7Y4/
Lesson 2 - Arrays
- OddOccurrencesInArray - https://codility.com/demo/results/trainingFN5RVT-XQ4/
- CyclicRotation - https://codility.com/demo/results/trainingSH2W5R-RP5/
Lesson 3 - Time Complexity
- FrogJmp - https://codility.com/demo/results/training6KKWUD-BXJ/
- PermMissingElem - https://codility.com/demo/results/training58W4YJ-VHA/

Keybase proof

I hereby claim:

  • I am codejockie on github.
  • I am codejockie (https://keybase.io/codejockie) on keybase.
  • I have a public key ASCRr5z1RHJ2EqiU25TuXRQHvlNVEbzC4tITsZGRLmd-LQo

To claim this, I am signing this object:

@codejockie
codejockie / js-modulus-fix.js
Last active November 12, 2020 11:30
How to handle JS modulus operator
// The following fixes the inconsisties in the JS % (modulus) operator when used on fractional numbers
// Example 1
// Multiply both sides of operand by 10000
let remainder = (100 * 10000) % (0.33 * 10000);
// Example 2
// Divide the numbers, then apply the modulus of result by 1
remainder = (100 / 0.33) % 1;
// Redux basics
// Redux is made up of 3 key parts, namely:
// Store
// Reducer
// Action/ActionCreator
const store = {
cartReducer: {},
profileReducer: {},
};
@codejockie
codejockie / crlf-to-lf.sh
Created March 23, 2021 21:15
DOS2Unix Line Endings (End Of Line)
# brew install dos2unix
find . -name '*.*' -print0 |xargs -0 dos2unix
@codejockie
codejockie / download.js
Created October 5, 2021 18:25
Download a file in JS
function download(fileUrl, filename) {
const a = document.createElement("a")
a.href = fileUrl
a.setAttribute("download", filename)
a.click()
}
// With Axios
import axios from "axios"
@codejockie
codejockie / Adapter.cs
Created December 13, 2023 21:15
Design Patterns
var hole = new RoundHole(5);
var rPeg = new RoundPeg(5);
hole.Fits(rPeg).Dump(); // True
var sm_sqpeg = new SquarePeg(5);
var lg_sqpeg = new SquarePeg(10);
//hole.Fits(sm_sqpeg); // Error: won't compile (incompatible types)
var sm_sqpeg_adapter = new SquarePegAdapter(sm_sqpeg);
var lg_sqpeg_adapter = new SquarePegAdapter(lg_sqpeg);
@codejockie
codejockie / TimeAgo.cs
Created December 29, 2023 20:58
A method to generate time elapsed since a given DateTime
void TimeAgo(DateTime date)
{
// 1000 millisecs = 1 sec
// 60 secs = 1 min
// 60 mins = 1 hr
// 24 hrs = 1 day
var diff = DateTime.Now - date;
var millis = diff.TotalMilliseconds;
var millisInDay = 86400 * 1000;
var daysInMillis = millis / millisInDay;
@codejockie
codejockie / Debouncer.cs
Created January 8, 2024 15:11
A simple debouncer
using System;
using System.Threading;
public class Debouncer : IDisposable
{
private Thread thread;
private volatile Action action;
private volatile int delay = 0;
private volatile int frequency;