mardi 26 novembre 2019

JS inserting objects correctly

I am trying to make miny pocket pokemon game. Here is how I am storing the pokemon.

let Charmander = {
        "name": "Charmander",
        "type": "fire",
        "hp": "70",
        "weaknesses": ["ground", "rock", "water"],
    };
let Bulbasaur = {
        "name": "Bulbasaur",
        "type": ["Grass", "Poison"],
        "hp": "70",
        "weaknesses": ["Fire", "Flying", "Ice", "Psychic"],
    };
let Squirtle = {
        "name": "Squirtle",
        "type": "Water",
        "hp": "60",
        "weaknesses": ["Electric", "Grass"],
};

It then asks you to pick which starting pokemon you want.

let startingChoice = prompt("What is your starting Pokemon " + Charmander.name + ", " + Bulbasaur.name + ", " + Squirtle.name);

I then create an array for the first pokemon that can be picked

let firstPokemon = [ Charmander.name, Bulbasaur.name, Squirtle.name];

I thennnn check the result of what you have typed in to see if it is in there by doing this.

if(startingChoice == "Charmander"){

    pokeBox.push(Charmander);

} else if(startingChoice == "Bulbasaur"){
    pokeBox.push(Bulbasaur);
}else{
    pokeBox.push(Squirtle);
}



}else{
    console.log("Chupa");
}

As you can see above that is pretty inefficient. How can I make it so that when it gets put into your pokeBox it adds in all of the objects info without the need of all of the info statements.

This is the result of pokeBox Object { name: "Charmander", type: "fire", hp: "70", … } How can I make this more efficient?

Aucun commentaire:

Enregistrer un commentaire