0

Alright, so why am I asking this question? Because I am making a simple evolution simulator. In it, each creature will get a random amount of food each generation. The amount of of food each creature gets is crucial to if it survives or not. Since the only way I see it working is in an array(and I'm not good at arrays) can you help me find a way to assign these numbers to objects within the array?

I've looked through multiple websites for answers and none hit the dot. I also don't have any code so can you submit some code so I can see what I have to do?

3
  • 2
    Hi! Please take the tour (you get a badge!) and read through the help center, in particular How do I ask a good question? Your best bet here is to do your research, search for related topics on SO, and give it a go. If you get stuck and can't get unstuck after doing more research and searching, post a minimal reproducible example of your attempt and say specifically where you're stuck. People will be glad to help. Commented Jul 28, 2019 at 17:19
  • If you are not "good in arrays" the chances that you can actually create even a simple "evolution simulator" is very slim, dont take this the wrong way... Nevertheless provide as with some sample code of what you already have, what is your structure, what is your "creature" object like etc etc... Commented Jul 28, 2019 at 17:21
  • This is more of a preparation project, with no selection whatsoever. I just need to find a way to randomize the numbers so I can be on the right track. Commented Jul 28, 2019 at 17:25

2 Answers 2

1

You can just loop over the array and assign a random value to each creature. Example:

let creatures = [
  {name: "Bob", food: 0},
  {name: "Alice", food: 0},
  {name: "Steve", food: 0}
];

for(let creature of creatures)
  creature.food = Math.random(); // random number for food between 0-1
  
console.log(creatures);

Sign up to request clarification or add additional context in comments.

Comments

0

Simpley Do:

const creatures = [{
        name: "Bob"
    },
    {
        name: "Alice"
    },
    {
        name: "Steve"
    }
];
const creaturesWithFood = creatures.map((creature) => {
    return {
        food: Math.floor(Math.random() * 20),
        ...creature
    }
});
console.log(creaturesWithFood);

I've limit the numbers to be less than 20...you can change it as per your needs, Hope this helps :)

1 Comment

pleasure.. Happy coding :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.