Daemonique
Daemonique

Reputation: 648

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();
    }
}

}

Upvotes: 0

Views: 1854

Answers (2)

ISD
ISD

Reputation: 1012

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())
        }
    }
}

Upvotes: 0

brk
brk

Reputation: 50291

You dont need to use keyword var. ALos in js this line for (int i is invalid. There is no int in js

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

Upvotes: 1

Related Questions