Search code examples
javascriptsyntax-errorvarp5.js

Uncaught syntax error unexpected identifier in JS and Html


I was trying out the p5.js when I got an uncaught syntax error. I have scanned all of the code several times but I can't get it for the life of me. Thank you for your effort in advance!

class Population {
var mutRate; // ERROR LINE
var population;        
function Population(pop, m) {
    mutRate = m;
    population = new DNA[pop];
    for (int i = 0; i < population.length; i++) {
      population[i] = new DNA();
    }
}

}


Solution

  • Maybe, you are making a class with constructor? It can be like this:

    class Population {
        // Javascript classes do not support any pre-declared fields
        // Maybe, as population function you meant constructor?
        constructor(pop, m){
            this.mutRate = m; // Use "this" to acess to object properties
            this.population = [] // Javascript not supports types
            // So we will create an empty array
            // And fill it with objects
            for(let i = 0; i < p.length; i++){
                this.population.push(new DNA())
            }
        }
    }