javascript - 我无法找到此代码的解决方案这些是问题
问题描述
- 编写一个函数,以 pokemon 的名称为参数并显示 pokemon 的信息
- 编写一个函数,将 pokemon 的名称作为参数,并找出所有 pokemon 在其“next_evolution”字段中具有该名称的所有 pokemon。
- 编写一个函数,将“弱点”作为输入,并给出所有具有该弱点的口袋妖怪的名称。(检查数据集中的弱点数组)。
它没有显示任何错误,也没有找到解决方案......我需要帮助来解决这个问题,我是初学者,所以详细的逻辑或代码会有所帮助
var goPokemon = {
"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 DetailOfPokemon = function(name, goPokemon) {
for (var x in goPokemon.pokemon) {
if (goPokemon.pokemon[x].name == findname) {
var Detail = goPokemon.pokemon[x];
alert(Detail);
} else {}
}
var findname = window.prompt('Enter the name of Pokemon')
};
DetailOfPokemon(name, goPokemon)
解决方案
var findname = window.prompt('Enter the name of Pokemon')
位置不好。你比较goPokemon.pokemon[x].name
和findname
尚未findname
定义。
JSON.stringify()函数会将对象转换为 json 字符串(可能更容易显示信息)。
顺便说一句,如果您不需要else
指令,请不要编写它。
var goPokemon = {
"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 DetailOfPokemon = function(findname, goPokemon) {
for (var x in goPokemon.pokemon)
{
if (goPokemon.pokemon[x].name == findname)
{
var Detail = goPokemon.pokemon[x];
alert(JSON.stringify(Detail, null, 4));
}
}
};
var name = window.prompt('Enter the name of Pokemon')
DetailOfPokemon(name, goPokemon)
推荐阅读
- python - AWS lambda 一次放置对象多个图像
- dax - Dax 度量以突出切片器之间的差异
- reactjs - 安装旧版 Storybook
- snowflake-cloud-data-platform - 使用复制授权作为选项创建临时表
- angular - 使用组件注入时传递 formGroup
- python - 在python中合并两个列表
- java - 调整文本大小时在底部保留 JScrollPane (JTextPane) 滚动条?
- powershell - 如何通过使用带有 powershell 的 Octopus deploy 来触发 Jenkins 作业?
- vue.js - 如何编辑 vue-loader 以支持音频文件?
- python - 将 Python 中的 PSD 图旋转 90 度 - 第 2 部分