Javascript 为什么';这是循环功,我的变量等于零?

Javascript 为什么';这是循环功,我的变量等于零?,javascript,loops,iterator,Javascript,Loops,Iterator,这是我的第一篇文章。作为一名初级程序员,我当然希望我的解释能够很好地为您提供最好的案例。提前谢谢你的回答 案例: 我需要数一数一个段落中有多少个点和感叹号。段落作为单个字符串存储在变量中 为了找到点和感叹号的计数,我想使用For循环对字符串进行迭代,并在字符串经过它们时进行计数 let story = 'Last weekend, I took literally the most beautiful bike ride of my life. The route is called &quo

这是我的第一篇文章。作为一名初级程序员,我当然希望我的解释能够很好地为您提供最好的案例。提前谢谢你的回答

案例: 我需要数一数一个段落中有多少个点和感叹号。段落作为单个字符串存储在变量中

为了找到点和感叹号的计数,我想使用For循环对字符串进行迭代,并在字符串经过它们时进行计数

 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.';
首先,我创建了一个变量,将单个字符串转换为每个符号的数组

const storySigns = story.split('');
其次,我创建了另一个变量来存储点和感叹号的计数

let puncCount = 0;
第三,我创建了一个for循环来迭代整个变量storySigns,并在每次循环通过时将puncCount增加1。或者

for (let i = 0; i < storySigns.length; i++)
 {
  if (i === '.' || i === '!') {
    puncCount += 1;
  }
};
for(设i=0;i
最后,我将puncCount的结果记录到控制台,结果是0。这显然不是我所希望的结果

谁能解释一下为什么这个策略不起作用。我已经有了另一个有效的策略,但是我想知道为什么这是错误的

提前谢谢


Maurice

I是一个计数器,这里它的值是0,然后是1,然后是2,然后是3,直到n
它永远不会等于“.”或“!”

i
是一个数字,不是点或感叹号。你需要
故事符号[i]