-5

I am unable to do this

Write a function to take pokemon’s name as argument and display the information of that pokemon

My code is

var FullGame = {
  "pokemon": [{
    "id": 1,
    "num": "001",
    "name": "Bulbasaur",
    "img": "http://www.serebii.net/pokemongo/pokemon/001.png",
    "type": [
      "Grass",
      "Poison"
    ],
    "height": "0.71 m",
    "weight": "6.9 kg",
    "candy": "Bulbasaur Candy",
    "candy_count": 25,
    "egg": "2 km",
    "spawn_chance": 0.69,
    "avg_spawns": 69,
    "spawn_time": "20:00",
    "multipliers": [1.58],
    "weaknesses": [
      "Fire",
      "Ice",
      "Flying",
      "Psychic"
    ],
    "next_evolution": [{
      "num": "002",
      "name": "Ivysaur"
    }, {
      "num": "003",
      "name": "Venusaur"
    }]
  }, {
    "id": 2,
    "num": "002",
    "name": "Ivysaur",
    "img": "http://www.serebii.net/pokemongo/pokemon/002.png",
    "type": [
      "Grass",
      "Poison"
    ],
    "height": "0.99 m",
    "weight": "13.0 kg",
    "candy": "Bulbasaur Candy",
    "candy_count": 100,
    "egg": "Not in Eggs",
    "spawn_chance": 0.042,
    "avg_spawns": 4.2,
    "spawn_time": "07:00",
    "multipliers": [
      1.2,
      1.6
    ],
    "weaknesses": [
      "Fire",
      "Ice",
      "Flying",
      "Psychic"
    ],
    "prev_evolution": [{
      "num": "001",
      "name": "Bulbasaur"
    }],
    "next_evolution": [{
      "num": "003",
      "name": "Venusaur"
    }]
  }, {
    "id": 3,
    "num": "003",
    "name": "Venusaur",
    "img": "http://www.serebii.net/pokemongo/pokemon/003.png",
    "type": [
      "Grass",
      "Poison"
    ],
    "height": "2.01 m",
    "weight": "100.0 kg",
    "candy": "Bulbasaur Candy",
    "egg": "Not in Eggs",
    "spawn_chance": 0.017,
    "avg_spawns": 1.7,
    "spawn_time": "11:30",
    "multipliers": null,
    "weaknesses": [
      "Fire",
      "Ice",
      "Flying",
      "Psychic"
    ],
    "prev_evolution": [{
      "num": "001",
      "name": "Bulbasaur"
    }, {
      "num": "002",
      "name": "Ivysaur"
    }]
  }]
};



var name = prompt('Enter the name of Pokemon');

var DetailOfPokemon = function(name, FullGame) {

  for (var currentPokemon in FullGame) {
    if (FullGame.pokemon[currentPokemon].name == name) {
      var Detail = FullGame.pokemon[currentPokemon];
      alert(Detail);
    } else {
      alert("Type Again");
    }
  }
};
DetailOfPokemon(name, FullGame);
ADyson
  • 57,178
  • 14
  • 51
  • 63
Kunal
  • 23
  • 7
  • 2
    what does "unable to" mean? What happens to your code when you run it? – ADyson Feb 02 '18 at 16:09
  • After taking the input from the user, it does not show the detail of that pokemon – Kunal Feb 02 '18 at 16:11
  • Well that much I guessed. Technical error information and/or detailed description of the unexpected behaviour was what I meant. I've converted your code into a runnable snippet. Now you can probably see you have a JS error – ADyson Feb 02 '18 at 16:12
  • 1
    You currently loop at the root of you json, but there is juste 1 key there ("pokemon") and it has no name proprety. You need to loop on `FullGame.pokemon` – Liora Haydont Feb 02 '18 at 16:13
  • Possible duplicate of [Access / process (nested) objects, arrays or JSON](https://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json). Read this to understand how to work with JS objects, and use the [browser console (dev tools)](https://webmasters.stackexchange.com/q/8525) (hit `F12`) to see what all the different components are. – Sebastian Simon Feb 02 '18 at 16:16
  • @GalAbra but `currentPokemon` is an index here, not the name ! So this part of the code works – NatNgs Feb 02 '18 at 16:17

1 Answers1

1

You where comparing the wrong array in your code, you can fix it like following:

for (var currentPokemon in FullGame.pokemon) { // HERE added .pokemon at the end
  if (FullGame.pokemon[currentPokemon].name == name) {
    var Detail = FullGame.pokemon[currentPokemon];
    alert(Detail);
  } else {
    alert("Type Again");
  }
}
NatNgs
  • 874
  • 14
  • 25
  • please help in this problem Write a function that take a “Weakness” as an input and gives the names of all pokemon who have that Weakness. (check the weakness array in the dataset) – Kunal Feb 03 '18 at 09:19
  • this is my code var weakness = prompt('Enter the weakness'); var WeaknessName = function(weakness,FullGame) { for(var presentPokemon in FullGame.pokemon) { for(var j = 0; j < FullGame.pokemon.weaknesses.length; j++) { if(FullGame.pokemon[presentPokemon].weaknesses.toString() == weakness) { var pokemonName = FullGame.pokemon[presentPokemon].name; alert(pokemonName); break; } } } }; WeaknessName(weakness,FullGame); – Kunal Feb 03 '18 at 09:20
  • 1
    @Kunal ask a new question if you have a new question. But really you should be able to take the learning from this question and apply it to the new one, it's the same kind of process, just looking at another part of the data. – ADyson Feb 03 '18 at 19:05