我需要帮助,请检查程序是否有任何问题,请帮助我纠正它

时间:2019-02-06 05:13:50

标签: javascript css

<!Doctype html>     
<html>     
<title>JavaScript Tutorial</title>     
<body>     
<script language = "javascript">     
var cleanCities = ["Cheyenne ","Santa Fe ","Tucson ","Great Falls ","Honolulu"];     
var cityToCheck = "Tucson";

if(cityToCheck === cleanCities[i]) {     
  alert("You are clean");     
} 
</script> 

</body> 

</html> 

我找不到错误

这是一个JavaScript代码

<!Doctype html> 
<html> 
<title>JavaScript Tutorial</title> 
<body> 
<script language = "javascript"> 
var cleanCities = ["Cheyenne ","Santa Fe ","Tucson ","Great Falls ","Honolulu"]; 
var cityToCheck = "Tucson"; 
if(cityToCheck === cleanCities[i]) { 
  alert("You are clean"); 
} 
</script> 
</body> 
</html> 

它说第14行有问题

1 个答案:

答案 0 :(得分:1)

您要为其索引i的for循环在哪里?如果有意将其删除,则阵列中的Tucson末尾会有一个空格。使用修剪方法。

另外语言=“ javascript”是错误的,通常是type =“ text / javascript”

代码应如下所示:

enter code here
var cleanCities = ["Cheyenne ","Santa Fe ","Tucson ","Great Falls ","Honolulu"];
var cityToCheck = "Tucson";
for(var i=0; i<cleanCities.length; i++) {
   if(cityToCheck === cleanCities[i].trim()){
    alert('you are clean');
   }
 }