Q:

Write a JavaScript program to hash a given input string into a whole number

0

Write a JavaScript program to hash a given input string into a whole number.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

const sdbm = str => {
  let arr = str.split('');
  return arr.reduce(
    (hashCode, currentVal) =>
      (hashCode = currentVal.charCodeAt(0) + (hashCode << 6) + (hashCode << 16) - hashCode),
    0
  );
};
console.log(sdbm('w3r'));
console.log(sdbm('name'));

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now