1

I have a class object that I thought was working last night. But now I try to test out my set Hash method so I do: blah = new HashTable; and it says that HashTable isn't defined.

I have another file thats not linked here and all its variables are also showing up as undefined(in the console). I swear I got this working last night though so i'm miffed.

I'm getting an error that maybe I didn't notice before but it says

ERROR: Parsing error: The keyword 'class' is reserved class HashTable

JSLint(2) Expected an identifier and instead saw 'class'. Stopping. (4% scanned).

What am I doing wrong? Someone please help.

My desire, is just to be able to test out my javascript code in the console to make sure that everything is working. I tried google chrome's snippet and it has bugs. I tried codepen and it's a sandbox so it doesn't work properly in the console now I'm trying brackets and it's not letting me test in the console either. Getting so frustrated.

class HashTable {
    constructor(size){
        this.keyMap = new Array(size);
    }

function _hash(key) {
    let total = 0;
    let prime = 31
    for (let i = 0; i < Math.min(key.length, 100); i++) {
        let char = key[i];
        let value = char.charCodeAt(0) - 96;
        total  = (total * prime + value) % this.keyMap;
    }
    return total;
   }

    function set(key){
        hashNumber = this._hash(key);
        return hashNumber;
    }

}

-

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src = "app.js"></script>
    <script src = "hash.js"></script>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <h1>Please work</h1>

</body>
</html>
Mugs
  • 339
  • 3
  • 14
  • 3
    Whatever tool is giving you the error on `class` is out of date. As of ES2015, `class` isn't just reserved, it's in use -- for what you're trying to use it for. Sounds like you need to update your tools, I'm sure the current version of JSLint (for instance) understands `class` syntax. – T.J. Crowder Sep 22 '19 at 13:59

2 Answers2

3

I don't know if it specifically relates to the errors you've called out or not, but your HashTable class has syntax errors in it. You don't use function to define the methods of a class within a class construct. So:

class HashTable {
    constructor(size){
        this.keyMap = new Array(size);
    }

    _hash(key) {
//  ^--------------------------------------- no `function` here
        let total = 0;
        let prime = 31
        for (let i = 0; i < Math.min(key.length, 100); i++) {
            let char = key[i];
            let value = char.charCodeAt(0) - 96;
            total  = (total * prime + value) % this.keyMap;
        }
        return total;
    }

    set(key) {
//  ^--------------------------------------- nor here
        hashNumber = this._hash(key);
        return hashNumber;
    }
}

Other notes:

  • There's almost never any reason to use new Array.

  • You're using the array you've assigned to this.keyMap as a number in the expression total = (total * prime + value) % this.keyMap; which doesn't make any sense.

  • I'd recommend calculating the upper bound of your for loop once, and then reusing that value, rather than calculating it on every loop iteration.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Yes that fixed it completely. Thank you! I was dying. I'm still getting the error but at least its working now and that's all I care about. I also had variables declared in my other js file and I removed the var declaration and they started working as well. Your the best! – Mugs Sep 23 '19 at 15:03
  • oh I was using prime numbers because I heard theirs some math that makes the hash function give numbers more spread out and also makes the calculation faster. – Mugs Sep 23 '19 at 15:13
1

Not exactly true @T.J. Crowder: ESLint 7.11.0 with node 14.14 (currently the latest versions) will throw this error.

Even though ESLint is favored over JSLint (as mentioned here), it seems babel-eslint is preferred (as is mentioned with the how to here) and it will solve this problem.

DZet
  • 539
  • 3
  • 10