Created
February 28, 2013 20:40
-
-
Save syntacticsugar/5059926 to your computer and use it in GitHub Desktop.
Euler #1, in bloated, x-rated, nasty, perverse, lecherous, spaghetti Javascript ! (evil cackle)
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
// Find the sum of all the multiples of 3 or 5 below 1000. | |
// | |
// | |
// ruby version: (inspired by Lfborjas' Ruby Fuckery) | |
//multi = ->(x) { (x % 3).zero? or (x % 5).zero? } | |
//puts (1...1000).select{ |x| multi[x] }.inject(:+).to_s | |
//233168 | |
function arrayFromRange(lo, hi){ | |
var collection = []; | |
for (var i = lo; i <= hi; i++) { | |
collection.push(i); | |
} | |
return collection; | |
}; | |
function reduce(array, operation, initial){ | |
var results = initial; | |
for (var i = 0; i < array.length; i++) { | |
results = operation(results, array[i]); | |
} | |
return results; | |
}; | |
var hugeArray = arrayFromRange(1, 999); | |
function addStuff(x,y){ | |
return x + y; | |
}; | |
function multiMod(n){ | |
if ((n % 5 === 0) || (n % 3 ===0)) { | |
return n; | |
}; | |
}; | |
function euler(array){ | |
var collection = []; | |
for (var i = 0; i < array.length; i++){ | |
if ((multiMod(array[i]) !== undefined)){ | |
collection.push(array[i]); | |
}; | |
} | |
return collection; | |
}; | |
console.log(reduce( euler(hugeArray), addStuff, 0 )); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment