Skip to content

Instantly share code, notes, and snippets.

@ChanChar
Created May 28, 2015 02:57
Show Gist options
  • Save ChanChar/b7bbacaf358471a60a14 to your computer and use it in GitHub Desktop.
Save ChanChar/b7bbacaf358471a60a14 to your computer and use it in GitHub Desktop.
// write myMap(array, callback)
Assessment.myMap = function(array, callback) {
var mappedArr = [];
for (n=0; n < array.length; n++) {
mappedArr.push(callback(array[n]));
}
return mappedArr;
};
// write primes(n)
Assessment.primes = function(n) {
function isPrime(num) {
var prime = true;
if (num === 2) {
return true;
} else {
for (f = 2; f < num; f++) {
if (num % f === 0) {
prime = false;
}
}
}
return prime;
}
var primes = [];
var currentNum = 2;
while (primes.length < n) {
if (isPrime(currentNum)) {
primes.push(currentNum);
}
currentNum++;
}
return primes;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment