> 웹 프론트엔드 > JS 튜토리얼 > 일/코드 요일: JavaScript의 반복 능력 활용

일/코드 요일: JavaScript의 반복 능력 활용

王林
풀어 주다: 2024-09-08 20:36:09
원래의
1194명이 탐색했습니다.

Day /  Days of Code: Harnessing JavaScript’s Iterative Power

Fri, September 6, 2024

Hello everyone! ?

In a display of JavaScript prowess, today's project of a grammar checker employs iterative methods like .forEach(), .map(), and .filter() to facilitate the transformation and traversal of arrays, showcasing the strength and sleekness of JavaScript’s iterative power.

First, we're given a short story as a string; Then, the story is split into an array of words separated by spaces:

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. 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!.[edited for brevity]';

let storyWords = story.split(' ');
로그인 후 복사

Next up, is spelling and grammar checking. These examples are single words, although this could be extended with a .map() of common misspellings and language patterns:

let unnecessaryWord = 'literally';
let misspelledWord = 'beautifull';
let badWord = 'freaking';
로그인 후 복사

To update the story, we use iterator methods we've learned, including .filter(), .map(), .findIndex(), and .every(). Next, we remove the unnecessary word 'literally' using .filter() with an arrow function--as is common post-ES6. Note that storyWords is modified in place:

storyWords = storyWords.filter(word => {
  return word !== unnecessaryWord;
});
로그인 후 복사

Next, spelling corrections are applied via the .map() function, noting that .map() could be used more comprehensively with common misspelling and correction pairs:

storyWords = storyWords.map( word => {
  return word === 'beautifull' ? 'beautiful' : word;
});
로그인 후 복사

In the given scenario, one's grandmother is expected to read the passage, so the 'bad' word 'freaking' is replaced using .findIndex() and direct indexing.

let badWordIndex = storyWords.findIndex(word => word === badWord);
storyWords[badWordIndex] = 'really';
로그인 후 복사

Finally, a check is performed in consideration of reading ease, such as you may've seen in a Flesch reading score based on average word length; Here we're given that there's one word over 10 characters long, and will replace with 'glorious', using .forEach() to find the index, followed by direct replacement:

let index = 0;
storyWords.forEach(word, i) => {
  if (word.length > 10) index = i;
});
storyWords[index] = 'glorious';
로그인 후 복사

Clean and readable code is crucial not only because it makes code easier to understand and maintain, but it also reduces the likelihood of errors. This is especially important in collaborative environments where multiple developers work on the same codebase. Iterative methods like .forEach(), .map(), and .filter() are preferred over traditional loops because they offer a more declarative approach to coding. This means you can express what you want to achieve without detailing the control flow, leading to code that is more concise, easier to read, and less prone to errors.

Happy coding! ?

위 내용은 일/코드 요일: JavaScript의 반복 능력 활용의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

원천:dev.to
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿