Created
June 20, 2015 02:39
-
-
Save jasonalderman/981f170536deaa517413 to your computer and use it in GitHub Desktop.
Quick function to determine if your name adds up to a prime number.
This file contains 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
// **** | |
// Because I spent all week writing javascript, then I saw this: | |
// https://instagram.com/p/4DJNNbR5_P/ | |
// and needed to automate it, like so: | |
// > getNumber("Wolfgang Mozart") | |
// returns: "Your name is not a prime number: Wolfgang Mozart (178)." | |
// **** | |
var getNumber = function getNumber(nameString) { | |
var arr = nameString.toLowerCase().split(''); | |
var key = " abcdefghijklmnopqrstuvwxyz".split(''); | |
var count = 0; | |
for(var i=0; i<arr.length; i++) { | |
count += key.indexOf(arr[i]); | |
} | |
// Primes list from function: http://stackoverflow.com/a/12287599 | |
isPrimeIndex = ([2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293].indexOf(count) !== -1); | |
return "Your name is " + ["not "," "][isPrimeIndex*1] + "a prime number: "+nameString+" ("+count+")."; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment