如果數組的長度大於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