Skip to content

Instantly share code, notes, and snippets.

@devinrhode2
Created May 4, 2012 02:36
Show Gist options
  • Save devinrhode2/2591524 to your computer and use it in GitHub Desktop.
Save devinrhode2/2591524 to your computer and use it in GitHub Desktop.
Fun excercise I did with a tech co-founder
/*
Task:
----------------------------------------------------
Fill the array 'randomNumbers' with 64 random numbers.
----------------------------------------------------
Requirements:
* You must use the provided getRandomNumber function.
* Make sure that there are no duplicate numbers in the array.
* Try to keep the running time reasonable!
*/
// You can't change this function
function getRandomNumber(callback) {
var random = Math.floor( Math.random() * 70 );
setTimeout( function(){ callback(random); }, 1000);
}
var randomNumbers = [];
// Start your code below here
// -----------------------------------
randomNumbers.isAlreadyHere = function isAlreadyHere(someNumber) {
var length = randomNumbers.length
, result = false;
for (var spot = 0; spot < length; spot++) {
if (randomNumbers[spot] === someNumber) {
result = true;
}
}
return result;
};
for (var round = 0; round < 65; round++) {
getRandomNumber(function getRandCallback(random) {
while (randomNumbers.isAlreadyHere(random)) {
random++;
random %= 70;
}
randomNumbers[randomNumbers.length] = random;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment