gpt4 book ai didi

javascript - 使用 JavaScript 迭代器的困难

转载 作者:行者123 更新时间:2023-12-01 02:33:35 26 4
gpt4 key购买 nike

希望您今天过得愉快。我无法使用迭代器获得预期结果。我查看了有关迭代器的 MDN 文档,感觉好像我了解如何使用它们,但我可能会错,因为我对编码非常陌生。

下面是我的代码:

    let story =
'Last weekend, I took literally the most beautiful bike ride of my life. The route is called "The 9W to Nyack" and it actually stretches all the way from Riverside Park in Manhattan to South Nyack, New Jersey. It\'s really an adventure from beginning to end! It is a 48 mile loop and it basically took me an entire day. I stopped at Riverbank State Park to take some extremely artsy photos. It was a short stop, though, because I had a really long way left to go. After a quick photo op at the very popular Little Red Lighthouse, I began my trek across the George Washington Bridge into New Jersey. The GW is actually very long - 4,760 feet! I was already very tired by the time I got to the other side. An hour later, I reached Greenbrook Nature Sanctuary, an extremely beautiful park along the coast of the Hudson. Something that was very surprising to me was that near the end of the route you actually cross back into New York! At this point, you are very close to the end.';

let overusedWords = ["really", "very", "basically"];

let unnecessaryWords = ["extremely", "literally", "actually"];

let storyWords = story.split("");

console.log(storyWords.length);

let betterWords = storyWords.filter(function(word) {
if (!unnecessaryWords.includes(word)) {
return word;
}
});

console.log(betterWords);

let totalUsed = 0;

for (let i = 0; i < betterWords.length; i++){
if (betterWords[i] === overusedWords[0]) {
totalUsed++;
} else if (betterWords[i] === overusedWords[1]) {
totalUsed++;
} else if (betterWords[i] === overusedWords[2]) {
totalUsed++;
}
}

第一个预期结果 - console.log(betterWords) 语句应该打印出故事,而不包含不必要的单词中列出的单词。

第二个预期结果 - console.log(totalUsed) 语句应打印出这些单词在故事中出现的总次数。

目前,我从 console.log 语句中分别得到 978 和 0。

非常感谢任何帮助!

最佳答案

一些事情:

  • 您应该将故事字符串拆分为空格:"",而不是空字符串来获取字数。拆分空字符串:"",将为您提供一个包含每个字符的数组,而不是每个单词。
  • 数组.filter方法采用一个应返回 bool 值的函数。如果该 bool 值为 true,则保留该项目,否则将删除该项目。因此,如果该单词是不必要的单词,您的过滤器将返回该单词,但这是事实,因此您实际上只会保留不必要的单词。最好直接返回 .includes 调用的结果。
  • 最后,用于计算过度使用单词的 for 循环应该可以完美运行,但您也可以使用数组 .reduce 方法,因为它基本上非常适合该方法用例。

let story = 'Last weekend, I took literally the most beautiful bike ride of my life. The route is called "The 9W to Nyack" and it actually stretches all the way from Riverside Park in Manhattan to South Nyack, New Jersey. It\'s really an adventure from beginning to end! It is a 48 mile loop and it basically took me an entire day. I stopped at Riverbank State Park to take some extremely artsy photos. It was a short stop, though, because I had a really long way left to go. After a quick photo op at the very popular Little Red Lighthouse, I began my trek across the George Washington Bridge into New Jersey.  The GW is actually very long - 4,760 feet! I was already very tired by the time I got to the other side.  An hour later, I reached Greenbrook Nature Sanctuary, an extremely beautiful park along the coast of the Hudson.  Something that was very surprising to me was that near the end of the route you actually cross back into New York! At this point, you are very close to the end.';

let overusedWords = ["really", "very", "basically"];
let unnecessaryWords = ["extremely", "literally", "actually"];

// First, empty string split will separate the story into each individual character,
// when you want to get each word. To do that, split on a space character:
let storyWords = story.split(" ");
console.log("Story word count:", storyWords.length);

// In ES6, you can use an arrow function which is more concise. Also, the filter method
// should return a boolean. Your filter returns either the word itself (truthy) or
// undefined (falsey), and filter keeps the items we return true for, so your filter was
// actually keeping ONLY the unnecessaryWords.
let betterWords = storyWords.filter(word => !unnecessaryWords.includes(word));
console.log("A better story word count:", betterWords.length);
console.log("A better story:\n\n", betterWords.join(" "));

// For total used, this is the perfect place for a reduce:
let totalUsed = betterWords.reduce((count, word) => {
if (overusedWords.includes(word)) count++;
return count;
}, 0);

console.log("Total used:", totalUsed);

关于javascript - 使用 JavaScript 迭代器的困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48121483/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com