当名称全部大写时,函数应该向用户喊叫。例如,当名称为“JERRY”时,函数应返回字符串“HELLO, JERRY!”控制台记录错误:.toUpperCase() 不是函数。
var hello = "Hello, ";
function greet(name) {
if (name == null) {
console.log(hello + "my friend")
} else if (name == name.toUpperCase()) {
console.log(hello.toUpperCase() + name.toUpperCase())
} else {
console.log(hello + name);
}
}
var names = ["jack", "john"]
greet(names);
names
是一个数组。数组没有这样的功能。
你可能想在数组的每个元素上调用
greet
函数:
names.forEach(greet);
如果你想让
greet
函数接受一个数组作为参数,那么你 可以 做
function greet(name) {
if (Array.isArray(name)) {
name.forEach(greet);
return;
}
...
但是这种多态性通常被视为一种不好的做法。
您可以先申请
.toString()
,然后再使用.toUpperCase()
:
if (name === name.toString().toUpperCase())
names
是数组声明所以不能使用那种类型的函数,如果你想使用 for 循环或使用 names[1]
type 打印该数组
<script>
var hello = "Hello, ";
function greet(name) {
if (name == null) {
document.write(hello + "my friend")
} else if (name == name.toUpperCase()) {
document.write(hello.toUpperCase() + name.toUpperCase())
} else {
document.write(hello + name);
}
}
var names = ["jack", "john"]
greet(names[0]);
greet(names[1]);
</script>
var hello = "Hello, ";
function greet(names) {
for (var i = 0; i < names.length; i++) {
var name = names[i];
if (name == null) {
console.log(hello + "my friend")
} else if (name) {
console.log('toUpperCase works: ',hello.toUpperCase() + name.toUpperCase())
} else {
console.log(hello + name);
}
}
}
var names = ["jack", "john"]
greet(names);
如果将数组传递给函数,则不起作用。现在好多了。
因为
name
是一个数组,我认为你必须先循环它才能得到数组中的值。或者你可以试试我的代码:
var hello = "Hello, ";
function greet(name) {
//loop name with for of
for (let val of name) {
if (val == null) {
console.log(hello + "my friend")
} else if (val == val.toUpperCase()) {
console.log(hello.toUpperCase() + val.toUpperCase())
} else {
console.log(hello + val);
}
}
}
var names = ["jack", "john"]
greet(names);
另一种方式,更倾向于 ES,带有错误和类型处理:
function greet(names) {
const hello = "Hello"
if (!names){
console.log(`${hello} my friend`)
return;
}
// Handles a String
if (typeof names === "string") {
console.log(`${hello} ${name}`)
return;
}
// Error Handling for Array
if (Array.isArray(names) && !names.length) {
console.error("Passed Array is empty")
return;
}
names.map((name) => {
const str = `${hello} ${name}`
name == name.toUpperCase()
? console.log(str.toUpperCase())
: console.log(str)
// Optional if you have toProperCase as a prototype
// : console.log(`${hello} ${name.toProperCase()}`)
})
}
let names = ["jack", "JOHN"]
greet(names)
这将处理一个空数组、一个数组、一个字符串和空的或虚假的调用。如果您还需要处理一个对象,也可以使用额外的代码来完成。
奖金材料
避免讨论以这种方式使用原型是好是坏, 只是展示了一种合理的方式,如果需要的话可以做到。
toProperCase 原型:
String.prototype.toProperCase =
String.prototype.toProperCase ||
function (word) {
if (!word) {
word = this
}
if (word) {
let str = word.toLowerCase().split(" ")
for (let i = 0; i < str.length; i++) {
str[i] = str[i].charAt(0).toUpperCase() + str[i].slice(1)
}
return str.join(" ")
} else {
// ERROR message
// RETURNS var passed to it. If there is an issue it just returns the same value.
console.log(
"The util function toProperCase() \nis not able to do anything with '" +
word +
"' a typeof",
typeof word,
"\nReturning variable in same state."
)
return word
}
}