Question

Curious if I could get an example of a simple hash function in javascript as I...

Curious if I could get an example of a simple hash function in javascript as I am not sure what type of algorithm to use. I dont want the whole program just the function. Also if there could be some explanation of each line that would be great.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Let us consider the one of the simple hashing algorithm. Given a string the resultant hash of the function should be

hash = s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] where s is a string s[0] is a character at 0th position of string and n is the length of string. 

And here we need to ensure that output shouldnt be more than 32 bit.

Here is the javascript function below;

function simpleHash(str){
len = str.length; // len variable now contains str length
hash = 0; // initialising hash variable to 0
for(i=1; i<=len; i++){ // looping all over the length of str
char = str.charCodeAt((i-1)); // getting character at i-1 position of string
hash += char*Math.pow(31,(len-i)); // raising its value to 32^(len-1) and appending result to hash
hash = hash & hash; // this is javascript limitation to force to 32 bits if it crosses 32 bits
}
return hash; // finally we are returning the obtained hash value to callee function
}

console.log(simpleHash("CheggAnswer")); // to output the hash value in console

If we try to console the value of SimpleHash("CheggAnswer") below is the output results in console which is provided for proof of work. Output we get is 1041930555.

function simpleHash(str) len str.length; // len variable now contains str Length hash -0; // initialising hash variable to e for(i-1 ; i(zlen; ǐ++){ // looping all over the length of str char = str.charCodeAt((1-1)); // getting character at i-l position of string hash char Math.pow(31, (len-i)); II raising its value to 32(Len-1) and appending result to hash hash - hash & hash; // this is javascript Limitation to force to 32 bits if it crosses 32 bits return hash; II finally we are returning the obtained hash value to callee function console.log (simpleHash(CheggAnswer)); // to output the hash value in console 1041930555 VM83:12

Add a comment
Know the answer?
Add Answer to:
Curious if I could get an example of a simple hash function in javascript as I...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This should be in Java Create a simple hash table You should use an array for...

    This should be in Java Create a simple hash table You should use an array for the hash table, start with a size of 5 and when you get to 80% capacity double the size of your array each time. You should create a class to hold the data, which will be a key, value pair You should use an integer for you key, and a String for your value. For this lab assignment, we will keep it simple Use...

  • Purpose of the assignment To apply simple JavaScript concepts to create a Fahrenheit to Celsius (and...

    Purpose of the assignment To apply simple JavaScript concepts to create a Fahrenheit to Celsius (and Celsius to Fahrenheit) conversion program. I need both please! What’s required of you. Having looked at some basic examples of JavaScript on http://www.w3schools.com and at the “simple math with forms/inputs and validation” example in detail , I would like you to now apply those concepts to create a simple page that lets users type in some temperature value in the Fahrenheit/Celsius scale and when...

  • I really need assistance with creating a C++ hash table program for generating a hash from a string. Create a hash funct...

    I really need assistance with creating a C++ hash table program for generating a hash from a string. Create a hash function then test and evaluate the results. The set of strings are from a "words.txt" file (there are 45,402 words in the file, no repeating words. Map the words to 45,402 buckets using a hash function). Requirements: Create an array of 45,402 integers and initialize each to zero. These will be used to store how many times each index...

  • I really need assistance with creating a C++ hash table program for generating a hash from a string. Create a hash funct...

    I really need assistance with creating a C++ hash table program for generating a hash from a string. Create a hash function then test and evaluate the results. The set of strings are from a "words.txt" file (there are 45,402 words in the file, no repeating words. Map the words to 45,402 buckets using a hash function). Requirements: Create an array of 45,402 integers and initialize each to zero. These will be used to store how many times each index...

  • This question relates to Javascript. Could you please show me why my code is not working?...

    This question relates to Javascript. Could you please show me why my code is not working? I want to create 2 text boxes and have the user enter something into both of them. If the text entered into both boxes is identical the a green message is made visible. If the boxes are different a red message is made visable. Please keep it simple because I am new to javascript. <html> <head>               <title></title>        <script>...

  • This question relates to Javascript. Could you please show me why my code is not working?...

    This question relates to Javascript. Could you please show me why my code is not working? I want to create 2 text boxes and have the user enter something into both of them. If the text entered into both boxes is identical the a green message is made visible. If the boxes are different a red message is made visable. Please keep it simple because I am new to javascript. <html> <head>               <title></title>        <script>...

  • 1. In this lab, you will create a simple encryption function that will require a sentence...

    1. In this lab, you will create a simple encryption function that will require a sentence and a key (both strings) as a parameter and return an encrypted version of the string. The encryption algorithm will use the exclusive OR operator (commonly abbreviated as XOR). The general structure of the encryption is that every position in the sentence is XOR'd with the accompanying position of the key. If the sentence is longer than the key, you repeat the key. For...

  • C++ Give me an example of how you could use an if statement. The example should...

    C++ Give me an example of how you could use an if statement. The example should include a summary (Algorithm) of the program, and how you would use the if statement. I am including an example aswell. For example, suppose you had a program to determine if a number is positive, negative or zero. Read in a number and then use the if statement to determine if the number is positive (>0) or negative (<0) or zero (==0): if (x>0)...

  • Could you please help me write a Javascript code that will show a smiley face picture...

    Could you please help me write a Javascript code that will show a smiley face picture when I click the button. The picture should not display until the button is pressed. Here is my attempt at the code. It does not work. Please keep the code as simple as you can as I am new to coding. Please also use the <div> tag if possible, onclick and use the getElementById. Thank you <html> <body> <img id="exampleImage" src="smileyFace.png" /> <button onclick="pushButton()">Try...

  • JAVASCRIPT Create a simple web page that contains a JavaScript form that will allow the user...

    JAVASCRIPT Create a simple web page that contains a JavaScript form that will allow the user to answer 7 trivia questions. Your trivia game should contain: 2 text boxes 2 select dropdowns 2 multiple choice questions (using radio buttons, 4 options min) 1 choose-all-that-apply (checkboxes, 4 options min, one answer should be "None of the above"). No part-points for semi-correct answers. The questions can cover any topic you wish - but please keep it professional and easy enough that the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT