如果数组的长度大于1,那么我希望控制台记录所需的代码,但控制台会记录else语句。
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"])
答案 0 :(得分:1)
您将整个数组作为元素推送到另一个数组中。
<div class="x_content">
<canvas id="mybarChart"></canvas>
</div>
答案 1 :(得分:0)
您正在将一个项目推送到arrayNames中。它碰巧是一个数组,但它是一个数组,所以arrayNames.length = 1。
答案 2 :(得分:0)
答案 3 :(得分:0)
我在C#世界中比Javascript更多,但我认为你得到的是一个“类型”问题。您正在使用字符串数组调用greet函数。当你将它推入你的arrayNames时,arrayNames变成一个字符串数组的数组 - 也就是说,arrayNames的每个元素本身就是一个字符串数组,并且只有一个字符串,因为你只调用一次greet。
相反,尝试将<img src="http://www.planwallpaper.com/static/cache/a1/4e/a14e880ef245c3d159ba96ebbeb4c8c3.jpg">
<div>Changed img:</div>
<div><img src="http://www.planwallpaper.com/static/cache/a1/4e/a14e880ef245c3d159ba96ebbeb4c8c3.jpg"></div>
改为此,它应该有效(注意詹姆斯不会满足if条件,因为他是第一个):
greet(["James", "Garry", "JOSHUA", "steven"])
或者你可以想象并做一个for或foreach循环。