Javascript对数组元素的测试错误

Javascript workout error with array elements

本文关键字:测试 错误 数组元素 Javascript      更新时间:2023-09-26

我很想知道如何从数组中获得警报按摩下注元素数(巴西是6),而不是放入国家的名称。我试了好几种方法,但都不行。

HTML:

<body>
    <div></div>
</body>

Javascript:

var cleanCities = ["Argentina" , "Brazil", "Canada ", "Denmark"];
var visit = prompt("What is your city?");
var numLength = cleanCities.length;
var matchFound = false;
for (i=0; i<numLength ; i++) 
    if (visit === cleanCities[i]){
        matchFound = true;
        alert ("It is really a nice City")
        break;
    }
    if (matchFound === false){
        alert ("It is not in the List");
    }

首先,清理大括号和制表符:

var cleanCities = ["Argentina" , "Brazil", "Canada ", "Denmark"];
var visit = prompt("What is your city?");
var numLength = cleanCities.length;
var matchFound = false;
for (i=0; i<numLength ; i++) {
    if (visit === cleanCities[i]) {
        matchFound = true;
        alert ("It is really a nice City")
        break;
    }
}
if (matchFound === false){
    alert ("It is not in the List");
}


获取字符串的长度:

"Brazil".length
输出:6