Skip to content

Instantly share code, notes, and snippets.

@mitchallen
mitchallen / index.js
Last active December 30, 2021 14:14
JSON parser main function example
// Author: Mitch Allen
// File: index.js
import { parser } from './parser.js';
function main() {
let args = process.argv.slice(2);
if (args.length < 2) {
@mitchallen
mitchallen / schema.json
Created December 29, 2021 19:24
Example JSON Schema
{
"type": "object",
"properties": {
"foo": {
"type": "integer"
},
"bar": {
"type": "string"
}
},
@mitchallen
mitchallen / good.json
Created December 29, 2021 19:36
JSON input schema example
{
"foo": 1,
"bar": "abc"
}
@mitchallen
mitchallen / bad.json
Created December 29, 2021 19:43
JSON bad input schema example
{
"foox": 1,
"bar": "abc"
}
@mitchallen
mitchallen / parser.js
Created December 30, 2021 14:42
A less readable but more efficient parser
// Author: Mitch Allen
// File: parser.js
import { readFileSync } from 'fs';
import Ajv from 'ajv';
const ajv = new Ajv();
export function parser(inputFile, schemaFile) {
@mitchallen
mitchallen / coinflip.js
Last active January 2, 2022 11:31
A coinflip 50/50 chance example
// Author: Mitch Allen
// coinflip.js
let coinFlip = () => Math.round(Math.random());
const LIMIT = 100;
let list = [...Array(LIMIT)].map(() => coinFlip() ? "HEADS" : "TAILS");
const occurrences = list.reduce(function (acc, curr) {
@mitchallen
mitchallen / pickone.js
Created January 2, 2022 12:02
JavaScript example of random picking from a list.
// Author: Mitch Allen
// File: pickone.js
let pickOne = (list) => list[Math.floor(Math.random() * list.length)]
let LIMIT = 5;
let list = [...Array(LIMIT)].map(() => Math.random());
console.log(list);
// Author: Mitch Allen
// File: js-set.js
const X_SIZE = 100;
let a = [...Array(X_SIZE)].map(() => Math.random().toFixed(2));
let set = new Set(a);
console.log(a.length);
@mitchallen
mitchallen / coinflip.js
Created January 9, 2022 10:39
JavaScript Random Boolean Function
// Author: Mitch Allen
// File: coinflip.js
// coinFlip - return a random 1 or 0
export const coinFlip = () => Math.round(Math.random());
@mitchallen
mitchallen / test-coinflip.js
Created January 9, 2022 10:41
A file to test the coinflip.js module
// Author: Mitch Allen
// File: test-coinflip.js
import {coinFlip} from './coinflip.js';
// define test function for coinFlip()
function testCoinFlip() {
// define the number of coin flips to generate
const LIMIT = 100;