There seems to be an issue with this forEach loop as it is not functioning correctly

let wordsToRemove = ['extremely', 'literally', 'actually'];

textWords = text.split(' ')

let refinedWords = textWords.filter(function (word) {
  return !wordsToRemove.includes(word)
})

let reallyCount = 0
let basicallyCount = 0
let veryCount = 0

for (i = 0; i < textWords.length; i += 1) {
  if (refinedWords[i] == 'really') {
    reallyCount += 1}
  if (refinedWords[i] == 'basically') {
      basicallyCount += 1}
  if (refinedWords[i] == 'very' ) {
        veryCount +=1}
      }

console.log('Really Count : ', reallyCount)
console.log('Basically Count : ', basicallyCount)
console.log( 'Very Count : ', veryCount)

let sentenceCounter = 0

textWords.forEach(word =>{
  if(word[word.length - 1] === "." || word[word.length - 1] === "!"){
    sentenceCounter += 1
  }
});

console.log(sentenceCounter)

The expected count should be 12 but only shows 1.

The sentences counting function has a mistake. Any suggestions on how to fix it? The problem lies in the last forEach loop. Is there an alternative solution to this issue?

Answer №1

Spelling error found in the code:

sentencesCount += 1

To improve readability, I recommend using the increment operator instead:

sentencesCount++;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Utilizing CSS and JQUERY for Styling Border radius

Is there a way in jQuery to select specific parts and apply a "border radius"? I've been struggling to use a jQuery selector to achieve this. Is there another method using CSS? I don't want to manually add a "CLASS" to the HTML code to solve th ...

Maintaining input focus in AngularJS when a button is clicked

How can I ensure that the focus remains on the textarea when clicking a button inside a dynamically generated directive template? <input type="text" ng-model="myText" id = "area1" ></input> The buttons are created dynamically using a dire ...

The function driver.switchTo.frame() does not exist

I am a beginner with Selenium and facing an issue with a task I need to accomplish: Go to https://pastebin.com Paste "Hello from WebDriver" Set the paste expiration to 10 Minutes //Struggling with this step Set the paste title as "helloweb" I am using Ja ...

How can you determine the status of the display element affected by .slideToggle()?

I have attempted to use is(':visible'), is(':hidden'), and css('display') but I am unable to retrieve the display status of an element after applying .slideToggle() What could I be overlooking? Here is the jsfiddle: http://j ...

What is the best way to eliminate excess space on the right side in Bootstrap 4 while in mobile view?

I'm struggling to understand why this layout is not responsive on mobile devices, triggering a bottom scroll bar at around 616px. I'm looking for a solution to hide the scroll bar at least until 414px for iPhones and other smartphones. I've ...

How to eliminate the ng-component tag from an Angular 8 table row template

I currently have a dynamic table component with 2 modes: Normal table - functioning properly Custom Row Template - encountering issues due to Angular adding the <ng-component> tag The logic within the TableComponent is not the main concern, it&apo ...

Struggling with accurately configuring the input field in AngularJS?

Struggling with clearing the input field I have been attempting to clear the input box with ng-model="newInstruction.instructionText" after a new instruction text is added. However, the input field does not clear even after trying to reset it to an empty ...

Error: Unable to access the 'category_affiliation' property of null

After implementing a login function using redux state, I encountered an issue upon logging in. The error message TypeError: Cannot read properties of null (reading 'category_affiliation') is being displayed in my Sidebar.jsx file. It seems like t ...

What causes this specific error to produce a blank response, unlike the other errors that are functioning properly?

During my development project with NestJS, I ran into a challenge while handling errors in a service and controller for a specific component. In my code, I utilized try-catch methods to catch errors and then throw them. However, there was one particular er ...

Save the name and assigned value of a Button into a specific row within a MySQL table

I've been stuck on this issue for 3 days and it's really starting to frustrate me. It seems like a simple problem, but being a beginner, I just can't figure it out. I want to utilize the onclick function of a button to extract two specific ...

Tips for emphasizing specific text within HTML tags without highlighting the tags in Vue

I am using a tag with v-html to render HTML text and display it, like so: <div v-html="htmlText"></div> I have written code to highlight text and it works on regular text: Vue.filter('highlight', function (word, query) { ...

Update the controller to modify the route parameter

I've defined my state like this: stateProvider .state('app', { templateUrl: 'views/layout.html', url: '/:lang/app', resolve: { lang: ['$rootScope', &a ...

The position of a jQuery element gets reset when both the show and stop animations are used

When I use jQuery's .position() to place an element, everything works fine. But as soon as I display an animation and then cancel it with an ajax call, the position of the element resets. function displayMessage(msg) { var $messageEl = $('#u ...

What is the process for filling a table with data sourced from Firebase?

I have successfully used the code below to populate a datatable table with data from a JSON file. However, I am now attempting to populate the table with data from Google's Firebase database. The issue arises because new data pushed into Google's ...

Is there a way to implement a setTimeout delay in my puppeteer browser?

I wrote a function named waitForTimeout to introduce a brief delay that can be awaited once the browser navigates to a new page. function waitForTimeout(timeout) { return new Promise((resolve) => { setTimeout(resolve, timeout) }) } const puppet = () =& ...

Obtain the clicked href value and pass it from one page to another

Welcome, everyone, I am currently in the process of creating a webpage that serves the following functions: When the administrator accesses the page, they will see a form populated with information fetched from a database: https://i.sstatic.net/RrE9l.png ...

Razor: Embedding an @if statement within a tag and subsequently assigning an attribute

<tr @if (-- condition --){id="x"}> I understand that the code above may seem confusing. However, it serves as an example of what I am trying to achieve. My goal is to use Razor syntax to set the id attribute based on a certain condition. This is ne ...

Looking for the Mustache template library?

Can you provide some examples of how mustache can be used effectively? I recently came across it, but I'm struggling to grasp how it differs from the traditional approach of creating template files within frameworks like cakePHP or django, or simply ...

Retrieve the date and add one day to it

Currently, I am faced with the task of altering someone else's code, which is why I have to take a roundabout approach. Unfortunately, there are no other options available to me. In my jQuery script, I have a variable defined as var calc_date_to = jQ ...

Enhance the current MultiSelect object by incorporating JQuery MultiSelect

Is there a way to detect changes on a JQuery MultiSelect in order to trigger an update elsewhere? The typical javascript onchange method does not seem to work because the select itself does not change. However, I came across a method called "beforeclose" t ...