编码新手,我缺少什么

  • 本文关键字:新手 编码 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语句中。

最后,如果满足此条件,则返回与name级联的Hello

否则什么都不返回,所以它是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!"`)

在这种情况下,变量numbers实际上是未定义的。

此外,过滤器在这种情况下也没用。Filter主要用于从数组中获取与条件匹配的元素。

您应该使用if语句来检查长度是否相等。更好的是,您可以使用三元运算符。这里有一个例子:

function helloYou(name) {
return name.length % 2 === 0 ? 'Hello, ' + name : undefined;
}
console.log(helloYou('Bob'));
console.log(helloYou('Anna'));

相关内容

最新更新