Untitled

mail@pastecode.io avatar
unknown
plain_text
19 days ago
449 B
1
Indexable
Never
function stringHash(str: string): number {
  let hash = 0;
  if (str.length === 0) return hash;

  for (let i = 0; i < str.length; i++) {
    const char = str.charCodeAt(i);
    hash = ((hash << 5) - hash) + char;
    hash = hash & hash; // Convert to 32bit integer
  }

  return hash;
}

// Example usage
const myString = "This is a string";
const hashValue = stringHash(myString);

console.log(hashValue); // Output: A number (hash of the string)
Leave a Comment