javascript - 编码新手,我错过了什么?
问题描述
在 JS 上工作,我错过了什么?谢谢
修改下面的函数,只问候名字中字母为偶数的人
function helloYou(name)
numbers.filter (n => n % 2 =i= 1);{
}
/* Do not modify code below this line */
console.log(helloYou('Bob'), `<-- should return undefined`)
console.log(helloYou('Anna'), `<-- should return "Hello, Anna!"`)
解决方案
要访问字符串中的字母数,可以使用属性.length
。
然后检查这个数字是否是一个模数 2 应该返回 0,这就是我们需要检查的。这个条件在一个if
语句中。
最后,如果满足此条件,则返回Hello
与 连接name
。
否则什么都不返回,所以它是undefined
(不需要显式地写return undefined
)。
function helloYou(name) {
if (name.length % 2 === 0) {
return "Hello, " + name;
}
}
/* Do not modify code below this line */
console.log(helloYou('Bob'), `<-- should return undefined`)
console.log(helloYou('Anna'), `<-- should return "Hello, Anna!"`)
推荐阅读
- python - 我该如何解决这个功能?
- c# - UWP:如何在不减速的情况下运行任务?
- typescript - 如果对象的布尔属性为真,Typescript 会阻止将对象添加到对象数组中
- javascript - 创建对象时函数关键字是可选的吗?
- spring-boot - 如何处理 Springboot 应用程序中的 401 未授权错误
- python-3.x - get_type_hints 使用本地类引发 NamError
- android - 安全存储的限制 - KeyChain 和 KeyStore
- c++ - rcpp和protobuf:Free宏的双重定义
- apache-nifi - 如何使用 NiFi 转换 Jsonfile 的字段?
- java - dataSnapShot.getChildren() 返回 null