Add weightedSample snippet

This commit is contained in:
Angelos Chalaris
2019-12-31 11:34:26 +02:00
parent 6ea98b02ad
commit 21b0ab5c65
10 changed files with 164 additions and 95 deletions

View File

@ -0,0 +1,16 @@
const {weightedSample} = require('./_30s.js');
test('sample is a Function', () => {
expect(weightedSample).toBeInstanceOf(Function);
});
const arr = [3, 7, 9, 11];
const weights = [0.1, 0.2, 0.6, 0.1];
test('Returns a random element from the array', () => {
expect(arr.includes(weightedSample(arr, weights))).toBeTruthy();
});
test('Works for single-element arrays', () => {
expect(weightedSample([1], [1])).toBe(1);
});
test('Returns undefined for empty array', () => {
expect(weightedSample([], [])).toBe(undefined);
});