germancin
5/30/2018 - 2:36 AM

https://www.honeyfund.com/Careers/code-sample solution for code challenge Write a JavaScript function unhash(num) to find a ten letter strin

https://www.honeyfund.com/Careers/code-sample solution for code challenge Write a JavaScript function unhash(num) to find a ten letter string of characters that contains only letters from: acdfgilmnoprstuw such that the hash(the_string) is: --> 292681030017238 <--

const letters = 'acdfgilmnoprstuw';
const magicNum = 23;

function hash(string) {
  hashed = 7;
  for (let i = 0; i < string.length; i++) {
    hashed = hashed * magicNum + letters.indexOf(string[i]);
  }
  return hashed;
}

function unhash(hashed) {
  let decoded = '';
  let strPos = [];

  for (let i = 0; hashed > magicNum; i++) {
    strPos[i] = Math.floor(hashed % magicNum);
    hashed /= magicNum;
  }

  for (let i = strPos.length - 1; i >= 0; i--) {
    decoded += letters[strPos[i]];
  }
  return decoded;
}

console.log(hash('tortilla')); // 593846452632
console.log('--------');
console.log(unhash(hash('tortilla'))); // tortilla
console.log('--------');
console.log(unhash(292681030017238));