Ask coding questions

← Back to all posts
Why does this come out as undefined?(javascript)

Im making an AI Genetic Algorithm, to learn more about it click here and im trying to save the fitness value but it comes up as udefines when i try to log it in the console. I originally had saved a .fitness value to the agent then pushed the agent to the list like this:

var agent;
var fitness = 0;

var organisms = [];

agent = //whatever; = //the fitness level.


but it didnt work

so now im stuck and cant do it, i originally just gave up for a few days, stalling then i remembered, oh yeah, replit talk! SO someone pls help

oh yeah i forgot heres the code:

Answered by notGilbert (78) [earned 5 cycles]
View Answer
notGilbert (78)

You need to set agent to a not-undefined value

right now, you are just setting a property of undefined (which is not possible) and therefore will return undefined if you try to log it

or you can just declare agent with the fitness property

const agent = {
  fitness: 10

// ...

didnt fix it
it still says undefined


nvm it worked but now all fitness values come out as 5 @notGilbert


oh i forgot to add its better if u use the console coz graphics arent working at the moment

NehalRam (6)

i don't know akhid