Created
March 9, 2017 04:55
-
-
Save a-laughlin/49fc33235cec40adce8cb58994c8ea90 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(){ | |
// problem 1 | |
const curryJoin = a => b =>`${a}-${b}`; | |
// test 1 (expected: 'foo-bar') | |
console.log(curryJoin('foo')('bar')); | |
// problem 2 (not the cleanest solution, but not bad for hand-written with a 5 minute time constraint) | |
function pickGroup(groupsCollection){ | |
const resultArray = groupsCollection.reduce((arr, obj)=>{ | |
arr.push(...Array(obj.weight).fill(obj.group)); | |
return arr; | |
}, []); | |
return resultArray[Math.floor(Math.random() * resultArray.length)]; | |
} | |
// test 2 (expected: red ~50% blue ~25% green ~25%) | |
const groups = [{group:'red',weight:2}, {group:'blue',weight:1}, {group:'green',weight:1}]; | |
const iterations = 100000; | |
const colors={red:0,green:0,blue:0}; | |
let i=0; | |
let color; | |
while(++i<iterations){ colors[pickGroup(groups)] += 1; } | |
for(color in colors){ console.log(color, colors[color]/iterations); } | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment