使用 JavaScript 从字符串中返回冗长的单词
问题
我们需要编写一个JavaScript函数来接收一个单词和一个数字的句子。该函数应返回一个包含大于数字指定长度的所有单词的数组。
输入
const str = 'this is an example of a basic sentence'; const num = 4;
输出
const output = [ 'example', 'basic', 'sentence' ];
因为这是仅有的三个长度大于4的单词。
示例
以下是代码-
const str = 'this is an example of a basic sentence';
const num = 4;
const findLengthy = (str = '', num = 1) => {
const strArr = str.split(' ');
const res = [];
for(let i = 0; i < strArr.length; i++){
const el = strArr[i];
if(el.length > num){
res.push(el);
};
};
return res;
};
console.log(findLengthy(str, num));输出结果[ 'example', 'basic', 'sentence' ]
热门推荐
10 对患者生日祝福语简短
11 结婚祝福语简短装备
12 周岁祝福语学生文案简短
13 订婚领证祝福语简短精辟
14 导师获奖祝福语大全简短
15 新婚购房祝福语简短精辟
16 牛年祝福语简短的爱人
17 送芒果的祝福语简短
18 送给学长毕业祝福语简短