mid: 1671762272068
nid: 0
tags: []
date: 2023-03-09 13:59
---
# 章序号/节序号/节/笔记序号
codecademy, Iterators
# 概念阐释
# 举例子
# 类比、比较与对比
# 问题
[Grammar Checker](https://www.codecademy.com/courses/introduction-to-javascript/projects/mini-linter)
*注意return的问题*
```js
let story = 'Last weekend, I took literally the most beautifull bike ride of my life. The route is called "The 9W to Nyack" and it 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 literally took me an entire day. I stopped at Riverbank State Park to take some artsy photos. It was a short stop, though, because I had a freaking long way 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 a breathtaking 4,760 feet long! I was already very tired by the time I got to the other side. An hour later, I reached Greenbrook Nature Sanctuary, an extremely beautifull park along the coast of the Hudson. Something that was very surprising to me was that near the end of the route you literally cross back into New York! At this point, you are very close to the end.';
let storyWords = story.split(' ');
let unnecessaryWord = 'literally';
let misspelledWord = 'beautifull';
let badWord = 'freaking';
```
1. 先将字符串变成数组
在 main.js 中,story 变量保存着我们将要编辑的段落。 为了编辑故事,我们在第3行把它变成一个数组。.split()方法用空格字符('')分隔故事字符串,并将每个单词存储为数组的一个元素。查看数组 我们将在整个课程中使用,将 storyWords 记录到控制台。查看 storyWords 数组后,在移动到下一个任务之前注释掉 console.log() 语句。
2. 让数组看起来像字符串
为了更好地比较原始故事和编辑后的故事,我们希望将编辑后的 storyWords 数组视为字符串。 要将 storyWords 数组改回可读字符串,我们可以调用 storyWords 的 .join() 方法。为 .join() 方法提供一个空格字符 (' ') 的参数,以在字符串中用空格分隔每个数组元素。将 .join() 方法调用作为 console.log() 语句的参数,以将最终故事记录到控制台。
3. 修改字符串
现在是时候开始通过操作 storyWords 数组来编辑故事了。 我们希望能够看到更改,因此请确保加入故事的 console.log() 是编辑器中的最后一行代码。首先,在使用 .join() 的 console.log() 语句上方 方法,创建一个名为 count 的变量,用于存储数字 0。在 count 的正下方,使用 .forEach() 方法迭代 storyWords 数组。 作为 forEach() 方法的参数,创建一个空函数用作回调函数。
4. 计算单词数量
对于 storyWords 数组中的每个单词,我们希望 count 变量递增 1。
在 .forEach() 方法的回调函数中添加一个名为 word 的参数,用于在迭代 storyWords 数组时用于存储当前元素。 每次 storyWord 迭代时,将 count 加一。
在 .forEach() 方法下方,记录计数以查看故事中有多少个单词。
*提示:`++`像for循环,第一个单词等于count0,第二个单词等于count1,以此类推,最后得出有多少字符。.length算的是字符数而不是单词数*
5. 过滤不需要的词
这个故事的字数为 181 有点长。 让我们过滤掉所有“字面意思”一词的实例,以缩短故事并删除不必要的词。 我们将通过应用 .filter() 方法将过滤后的故事**重新分配给同一个 storyWords 变量**! 在整个项目中,我们将使用这种方法为故事的每个修订版重新分配 storyWords 变量。在您记录计数变量的位置下方,重新分配 storyWords 变量以等于对 storyWords 数组调用 .filter() 方法。 给 .filter() 方法一个参数为 word 的回调函数。
*因为storyWords是let变量,所以可以重新分配,而不需要另命名,注意已给的unncecessaryWord变量*
在 storyWords 变量声明下方,**注意第 4 行的变量 unnecessaryWord**。我们要从故事中过滤掉 unnecessaryWord 的值。在 filter() 方法的回调函数体中,只有当 word 不等于 unnecessaryWord 时才返回它。**检查 控制台`console.log(storyWords.join(' '));`** 中的故事字符串,以确保它不包含“literally”一词。 “字面意思”的第一个实例以前在第一句话中。
6. 检查拼写错误
现在我们已经删除了不必要的单词,让我们处理故事中任何拼写错误的单词。 我们可以使用 .map() 方法将 storyWords 重新分配给一个新的拼写检查单词数组!重新分配 storyWords 等于在 storyWords 数组上调用 .map() 方法。word 作为 .map() 的参数 回调函数。在回调的主体中,定义一个条件语句来检查单词参数是否等于 misspelledWord 变量。 如果是,则返回字符串的正确拼写“beautiful”。 如果不是,则返回单词。查看控制台中的故事字符串以查看正确的拼写! 您可以在故事的第一句话中看到拼写检查单词的一个实例。
7. 找到bad word
哦哦,你的曾祖母要读这个故事,里面有一个“坏”字! 让我们将 .findIndex() 方法应用于 storyWords 以查找坏词的索引。首先声明一个名为 badWordIndex 的变量并将其设置为对 storyWords 数组调用 .findIndex()。.findIndex() 回调函数应该 检查每个单词,看它是否等于第 6 行声明的 badWord 变量,并返回找到的单词的索引。然后,将 badWordIndex 记录到控制台。
8. 替换bad word
现在我们有了坏词的索引,我们可以很容易地用更合适的东西替换它。使用括号表示法访问 storyWords 数组中具有 badWordIndex 索引的元素。 将访问的元素设置为更合适的字符串,'really'。保存代码并检查坏词是否已被替换。
9. 是否每个单词的字符数<10
最后,让我们简化故事中的文字以吸引更广泛的受众。 我们可以使用 .every() 方法确保故事中的每个单词不超过 10 个字符。 .every() 方法使用回调函数来测试数组中的每个元素是否都通过指定条件。 如果所有元素都通过,则返回 true,如果有一个元素未通过,则返回 false。首先,定义一个名为 lengthCheck 的变量,并将其设置为调用 storyWords 上的 .every() 方法。 在回调函数中,测试每个单词是否小于等于10个字符。记录长度到控制台查看结果。 如果记录为 true,则故事中的每个单词都包含 10 个或更少的字符。 否则,一个或多个单词的长度超过 10 个字符。
10. 找到>10个字符的那个单纯的位置并替换为stunning, dazzling, or glorious.
# 问题答案
[答案](https://www.codecademy.com/workspaces/63bcd35fccf79a776f65a4ae)
```js
let story = 'Last weekend, I took literally the most beautifull bike ride of my life. The route is called "The 9W to Nyack" and it 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 literally took me an entire day. I stopped at Riverbank State Park to take some artsy photos. It was a short stop, though, because I had a freaking long way 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 a breathtaking 4,760 feet long! I was already very tired by the time I got to the other side. An hour later, I reached Greenbrook Nature Sanctuary, an extremely beautifull park along the coast of the Hudson. Something that was very surprising to me was that near the end of the route you literally cross back into New York! At this point, you are very close to the end.';
let storyWords = story.split(' ');
let unnecessaryWord = 'literally';
let misspelledWord = 'beautifull';
let badWord = 'freaking';
// 3-4. editing the grammar
let count = 0;
storyWords.forEach(
word => {
return word === count++;// 像for循环,第一个单词等于count0,第二个单词等于count1,以此类推,最后得出有多少字符。.length算的是字符数而不是单词数量。
}
);
console.log(count);
//5-6. filtering words
storyWords = storyWords.filter(
word => {
return word !== unnecessaryWord;
}
);
// 7. replacing words
storyWords = storyWords.map(
word => {
if(word === misspelledWord){
return 'beautiful';
}else{
return word;
}
}
);
//8. bad word
let badWordIndex = storyWords.findIndex(
word => {
return word === badWord
}
);
console.log(badWordIndex);
// 9. 替换bad word
storyWords[78]='really'
//10. 坚持单词长度
let lengthCheck = storyWords.every(
word => {
word.length < 10;
}
);
//11. 找出那个字符数>10的索引位置
lengthCheck = storyWords.findIndex(
word => {
return word.length >= 10
}
);
console.log(lengthCheck);
storyWords[94] = 'glorious';
//last line
console.log(storyWords.join(' '));
```
# 备注(经验集/错误集)
## 经验集
## 错误集
## 参考资料