Counting Vowels in a String with JavaScript

JavaScript Logic Building: Counting Vowels in a String I’ve been focusing on strengthening my JavaScript fundamentals lately, and today I tackled a classic logic problem: Counting the total number of vowels within a long string. My Logic & Workflow: Deconstruction: I first used .split(" ") to break the string down into an array of individual words. Nested Iteration: I used .forEach() to loop through each word, then split those words again into an array of characters. Validation: By defining a vowelArr, I used the .includes() method to check if each character was a vowel. Tracking: Every time a match was found, the character was pushed into a totalVowel array, allowing me to easily get the final count via .length. The Code: JavaScript function findVowel(text) { const a = text.split(" "); const totalVowel = []; const vowelArr = ["a", "e", "o", "i", "u"]; a.forEach((t) => { const z = t.split(""); const x = z.forEach((y) => { if (vowelArr.includes(y)) { totalVowel.push(y); } }); }); console.log(totalVowel?.length); } findVowel( "JavaScript is an amazing programming language. It allows developers to create interactive web applications with ease. Learning to code opens up a world of endless possibilities and innovation!", ); Breaking down problems into smaller, manageable steps is one of the most rewarding parts of software development. It’s all about the journey of improving logic, one function at a time! Find more sloved tasks here:https://lnkd.in/gFAEdKEn #JavaScript #WebDevelopment #CodingJourney #ProblemSolving #SoftwareEngineer #FullStack #LearningToCode #Programming

  • text

To view or add a comment, sign in

Explore content categories