Question:
Create a method stupidHash, which takes a string and returns an int. It should sum of the squares of each character's value (obtained using charCodeAt and then subtracting 1), and return the division remainder of the sum when divided by 10.
These were the test examples given:
console.log(stupidHash("")); results 0console.log(stupidHash("abc")); results 5console.log(stupidHash("1")); results 0I'm a beginner with little knowledge in JavaScript. I tried my best but couldn't get the result.
My code:
function stupidHash(x){
let sum=0;
for(let i=0; i<x.length; i++){
sum = sum + Math.pow(x.charCodeAt(i) , 2) -1;
}
return sum%10;
}
console.log(stupidHash("abc"));
It seems you just forget the ++ after the last i in the for.
for(let i=0; i<x.length; i++)
I don't have verified the math, but it console log some numbers 😁