Take a look at this, use Math.random() to generate random index and get it from pokemons array, then unset the value from pokemons to have no doublon in pokedex.
let pokemons = ['pika1','pika2','pika3','pika4','pika5','pika6','pika7']
let pokedex = [];
for (i = 0; i < 5; i++) {
id = Math.floor(Math.random() * pokemons.length);
pokedex.push(pokemons[id]);
pokemons.slice(i,1)
}
//app.send() here...
console.log(pokedex)
EDIT: Considering you have a pokemon.random() generator(?)
in this npm package. Simply use:
let pokedex = [];
for(let i = 0; i < 5; i++)
pokedex.push(pokemon.random());
app.get('/dex', (req, res) => {
res.send(pokedex)
});
EDIT N°2: Using Objects
let pokedex = [];
for (i = 0; i < 5; i++) {
let pokemon = {
name:pokemon.random(),
attack:Math.floor(Math.random() * (100 - 50 + 1) +50),
defense:Math.floor(Math.random() * (100 - 50 + 1) +50)
};
pokedex.push(pokemon);
}
function attack(attacker,defenser)
{
defenser.defense -= attacker.attack
return attacker && defenser;
}
function fakeMatch(attacker,defenser)
{
attack(attacker,defenser)
}
console.log(pokedex)
fakeMatch(pokedex[0],pokedex[3])
console.log('RESULT Pokedex 0 attacked Pokedex 3')
console.log(pokedex)
EDIT N°3: Object with function (proper way)
let pokemons = ['pika1', 'pika2', 'pika3', 'pika4', 'pika5', 'pika6', 'pika7']
let pokedex = [];
for (i = 0; i < 5; i++) {
let id = Math.floor(Math.random() * pokemons.length);
let pkm = {
name: pokemons[id],
life: 100,
strenght: Math.floor(Math.random() * (100 - 50 + 1) + 50),
defense: Math.floor(Math.random() * (100 - 50 + 1) + 50),
attack: function(target) {
target.life -= this.strenght;
console.log(this.name + ' attack ' + target.name)
console.log(target.name + ' loose ' + this.strenght + ' of life')
//Test if life > 0 or anything else with % defense etc...
if (target.life <= 0) {
console.log(target.name + ' is dead')
}
}
};
pokedex.push(pkm);
pokemons.slice(id, 1)
}
pokedex[0].attack(pokedex[1]);