我正在尝试将大写元素记录到控制台,但是控制台每次都会抛出此错误:TypeError: arrayNames[i].toUpperCase is not a function
var hello = "Hello, ";
var arrayNames = [];
function greet(name) {
if (name == null) {
console.log(hello + "my friend");
}
//Requirement UpperCase
arrayNames.push(name);
for (var i = 0; i < arrayNames.length; i++) {
if (arrayNames[i] === arrayNames[i].toUpperCase()) {
console.log(hello.toUpperCase() + arrayNames[i].toUpperCase());
}
}
//Requirement last element
if (arrayNames.length > 1) {
var lastElement = arrayNames.pop();
console.log(hello + arrayNames + " and " + lastElement);
}
else {
console.log(hello + arrayNames);
}
}
greet(["James", "Julie", "BEN"]);
最佳答案
您正在对数组使用 toUpperCase。
问题是行:
arrayNames.push(name);
您正在创建一个多维数组。
使用
arrayNames = name;
相反。
关于javascript - 如何控制台数组中大写的日志元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42414501/