如果数组的长度大于1,那么我希望控制台记录所需的代码,但控制台会记录else语句。如果阵列的长度大于1,则执行
var hello = "Hello, ";
var arrayNames = [];
function greet(name){
if(name==null){
console.log(hello + "my friend")
}
//Requirement UpperCase
for (var i = 0; i < name.length; i++) {
if(name[i]===name[i].toUpperCase()){
console.log(hello.toUpperCase() + name[i].toUpperCase())
}
}
//Requirement last element
arrayNames.push(name)
if(arrayNames.length>1){
var lastElement = arrayNames.pop()
console.log(hello + arrayNames + " and " + lastElement)
}
else{
console.log(hello + arrayNames)
}
}
greet(["James", "Garry", "JOSHUA", "steven"])
你的数组长度是'1' - 你有一个数组,在第一个索引处包含另一个数组。 '.concat'会将两个数组合并在一起,'push'只是在最后一个索引处添加整个数组。 – tymeJV