Ask coding questions

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

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;
agent.fitness = //the fitness level.

organisms.push(agent);

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: https://replit.com/@JULIODIAZ1/Genetic-Algorithm

Answered by notGilbert (78) [earned 5 cycles]
View Answer
Comments
hotnewtop
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
}

// ...
JULIODIAZ1 (47)

didnt fix it
it still says undefined

JULIODIAZ1 (47)

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

JULIODIAZ1 (47)

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