任务1:将故事字符串分解为单个单词,并将它们存储在名为“ storyWords”的新数组中。
任务2:在storyWords数组中搜索不必要的单词,并创建一个排除它们的名为“ betterWords”的新数组。
任务3:计算storyWords数组中过度使用的单词的数量。
任务4:计算storyWords数组中的句子数量(计算“。”和“!”)。
这是我的尝试。我知道这写得很低。我可能使用太多的for循环。我想知道如何才能更有效地完成这4个任务。这是从一个代码学院课程中获得的。
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(' '); //**task 1**
const betterWords = [];
let count = 0;
let unnecessaryWordCount = 0;
let sentenceCount = 0;
for (i = 0; i < storyWords.length; i++) {
for (c = 0; c < unnecessaryWords.length; c++) { //**task 2:** creates new array, excluding unnecessary words.
if (storyWords[i] != unnecessaryWords[c]) {
count++
if (count == 3) {
betterWords.push(storyWords[i]);
count = 0;
break;
}
}
}
for (x = 0; x < overusedWords.length; x++) { //**task 3:** counts overused words.
if (storyWords[i] == overusedWords[x]) {
unnecessaryWordCount++;
}
}
if (storyWords[i].includes('.') || storyWords[i].includes('!')) { //**task 4:** count sentences.
sentenceCount++;
}
};
console.log(unnecessaryWordCount);
console.log(sentenceCount);