为了账号安全,请及时绑定邮箱和手机立即绑定

为什么“返回”不返回任何东西(JS)

为什么“返回”不返回任何东西(JS)

倚天杖 2022-10-21 10:50:09
我写了一个关于在单词中查找元音的代码。感谢console.log,我得到了输出,它可以工作。但是一旦我尝试返回,它不会返回任何东西..我不明白为什么?这里的代码:function vowelCount(str) {  let word = str.split("");  //   console.log(word);  //   console.log(word.length - 1);  let count = 0;  for (let i = 0; i <= word.length - 1; i++) {    if (      str[i] === "a" ||      str[i] === "e" ||      str[i] === "i" ||      str[i] === "o" ||      str[i] === "u" ||      str[i] === "y"    ) {      count = count + 1;    }  }  //   console.log(count);  return count;}vowelCount("hello");vowelCount("thereactor");
查看完整描述

4 回答

?
跃然一笑

TA贡献1826条经验 获得超6个赞

它正在回归。您只是没有使用返回值。这可以通过以下方式确定:

console.log(vowelCount("hello"));


查看完整回答
反对 回复 2022-10-21
?
慕斯709654

TA贡献1840条经验 获得超5个赞

确保使用返回的数据。

var vowelsInHello = vowelCount("hello");

console.log(vowelsInHello);


查看完整回答
反对 回复 2022-10-21
?
拉风的咖菲猫

TA贡献1995条经验 获得超2个赞

您的代码返回一个值。也许你没有调用这个函数。注意:使用字符串调用函数,例如:console.log("hello world");



查看完整回答
反对 回复 2022-10-21
?
白衣非少年

TA贡献1155条经验 获得超0个赞

这是一个更好的方法


function vowelCount(str) {

    let count = 0;

    for (let i = 0; i <= str.length - 1; i++) {

        var char = str.charAt(i).toLowerCase()

        if (

            char === "a" ||

            char === "e" ||

            char === "i" ||

            char === "o" ||

            char === "u" ||

            char === "y"

        ) {

            count++

        }

    }

    return count;

}

console.log(vowelCount('this has some vowels'))


查看完整回答
反对 回复 2022-10-21
  • 4 回答
  • 0 关注
  • 129 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号