-
-
Save Sharaf5/5d0714aa490095165ca88fbb054dfcca to your computer and use it in GitHub Desktop.
Full functions Hashing and Unhashing in Javascript...
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
//Write JavaScript code to find a ten letter string of characters that contains only letters | |
//I've been tested with this and here is the solution for those it may interest | |
/* | |
Here is the given pseudo code that I changed to retrieve my company's name | |
It's in 10 words. | |
I call it a Hasher | |
*/ | |
function hasher(hashedValue) | |
{ | |
var hash = 7; | |
var letters = "atdebilaeokrotum"; | |
for(var i = 0; i < hashedValue.length; i++) | |
{ | |
hash = (hash * 23 + letters.indexOf(hashedValue[i])) | |
} | |
return hash; | |
} | |
//the result is 292681030017238 | |
/*End Pseudo Code*/ | |
/* | |
Following is the solution | |
I call it the UnHasher function | |
*/ | |
function unhasher(hashNbr) { | |
var strpos = []; | |
var rst = 0; | |
var result = []; | |
var hash = hashNbr; | |
var letters = 'atdebilaeokrotum'; | |
for( i = 10; i > 0; i-- ){ | |
rst = ( hash % 23 ); | |
strpos[i] = rst; | |
hash = (hash - rst)/23; | |
result[i-1]= letters.charAt(strpos[i]); | |
} | |
return result.join(''); // array of chars to string | |
} | |
/* | |
End of my solution | |
Cheers! | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment