Skip to content

Instantly share code, notes, and snippets.

@mitchallen
mitchallen / index.html
Created January 13, 2022 03:09
Weighted choice Web app home page
<html>
<head>
<title>js-weighted-choice-01</title>
<link rel="stylesheet" href="./app.css">
</head>
<body>
<canvas id="canvas" width="300" height="300" />
<script type="module" src="./app.js">
@mitchallen
mitchallen / test-weighted-choice.js
Last active January 13, 2022 03:07
Test file for weighted choice
// Author: Mitch Allen
// File: test-weighted-choice.js
import { weightedChoice } from './weighted-choice.js';
function testWeightedChoice(source = {}) {
console.log('\nSOURCE:');
console.log(source);
@mitchallen
mitchallen / weighted-choice.js
Last active January 13, 2022 14:12
Selected an item from a weighted choice list
// Author: Mitch Allen
// File: weighted-choice.js
export function weightedChoice(source) {
let rnd = Math.random();
let lower = 0.00;
for (let choice in source) {
let weight = source[choice];
let upper = lower + weight;
if (rnd >= lower && rnd < upper) {
@mitchallen
mitchallen / app.js
Created January 11, 2022 09:22
Define a browser app function for display rollDice results
// Author: Mitch Allen
// File: app.js
import { rollDice } from './rolldice.js';
let canvas = document.getElementById("canvas");
const SCREEN_SIZE = 300;
const DIM = 10;
const CELL_SIZE = SCREEN_SIZE / DIM;
const BORDER = 1;
@mitchallen
mitchallen / app.css
Created January 11, 2022 09:20
Define a grid style for displaying the rollDice results
/**
* Author: Mitch Allen
* File: app.css
*/
canvas {
padding: 0;
margin: auto;
display: block;
width: 400px;
@mitchallen
mitchallen / index.html
Created January 11, 2022 09:18
Display rollDice results in a grid
<html>
<head>
<title>js-rollDice-01</title>
<link rel="stylesheet" href="./app.css">
</head>
<body>
<canvas id="canvas" width="300" height="300" />
<script type="module" src="./app.js">
// Author: Mitch Allen
// File: test-rolldice.js
import { rollDice } from './rolldice.js';
function testRollDice() {
// create a source list for testing
const source = ['A','B','C','D','E'];
// Author: Mitch Allen
// File: rolldice.js
export const rollDice = (list) => list[Math.floor(Math.random() * list.length)];
@mitchallen
mitchallen / app.js
Created January 10, 2022 03:34
Weighted Random Boolean visualizer app file
// Author: Mitch Allen
// File: app.js
import { weightedCoinFlip } from './weighted-coinflip.js';
let canvas = document.getElementById("canvas");
const SCREEN_SIZE = 300;
const DIM = 10;
const CELL_SIZE = SCREEN_SIZE / DIM;
const WEIGHT = 0.1;
@mitchallen
mitchallen / index.html
Created January 10, 2022 03:30
Weighted Code Flip visualizer index.html
<html>
<head>
<title>js-weightedCoinFlip-01</title>
<link rel="stylesheet" href="./app.css">
</head>
<body>
<canvas id="canvas" width="300" height="300" />
<script type="module" src="./app.js">