我无法执行下面的代码,因为它在输入时不返回输出。 [英] I am unable to execute the below code, as it does not returning the output while taking input.

查看:81
本文介绍了我无法执行下面的代码,因为它在输入时不返回输出。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var pokemongo=
{
  "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"
      ]
			  }]

	

};

var pokeinfo=function(name)
{
	var isfound=false;

	for(x in pokemongo){
		
		if(pokemongo[x]['name']==name){
			isfound=true;
			break;
		}
		else{
			isfound=false;
		}
	}// end of for in loop

if(isfound==true){
	
	alert(pokemongo.pokemon[x])
}
else{
	alert(" info not available ")
}

}


var name=prompt('Enter the name of pokemon')
pokeinfo(name)





我尝试了什么:





What I have tried:

<pre>var pokemongo=
{
  "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"
      ]
			  }]

	

};

var pokeinfo=function(name)
{
	var isfound=false;

	for(x in pokemongo){
		
		if(pokemongo[x]['name']==name){
			isfound=true;
			break;
		}
		else{
			isfound=false;
		}
	}// end of for in loop

if(isfound==true){
	
	alert(pokemongo.pokemon[x])
}
else{
	alert(" info not available ")
}

}


var name=prompt('Enter the name of pokemon')
pokeinfo(name)

推荐答案

x 不是中的索引pokemongo

尝试:

x is not an index in pokemongo
Try:
var pokeinfo=function(name)
{
	var isfound=false;

	for(x in pokemongo){
		
		if(x['name']==name){
			isfound=true;
			break;
		}
		else{
			isfound=false;
		}
	}// end of for in loop



您的代码没有按照您的预期行事,您不明白为什么!



几乎是通用的解决方案:逐步在调试器上运行代码,检查变量。

调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它不知道你应该做什么,它没有发现错误,它只是通过向你展示发生了什么来帮助你。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。

调试器 - 维基百科,免费的百科全书 [ ^ ]

JavaScript调试 [ ^ ]

Chrome DevTools &NBSP; |&NBSP;网络  |  Google Developers [ ^ ]

这里的调试器只显示你的代码正在做什么,你的任务是与它应该做什么进行比较。


Your code do not behave the way you expect, and you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]
JavaScript Debugging[^]
Chrome DevTools  |  Web  |  Google Developers[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


基于JSON对象,需要更新代码以使用多次迭代来使用JavaScript获得所需的结果。也。代码中有缺陷,例如,循环后x不可用/不在范围内。



这是一个例子



CP_find pokemon - JSFiddle [ ^ ]
Based on the JSON object, the code need to be updated to use several iteration to get the desire result using JavaScript. Also. there are flaws in the code, example, the x is not available/not in scope after the loop.

Here is an example

CP_find pokemon - JSFiddle[^]


这篇关于我无法执行下面的代码,因为它在输入时不返回输出。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆