Troubleshooting Regular Expressions in JavaScript

My objective is to analyze a string in order to identify the presence of lowercase letters or numbers, and then add that specific letter or number to an array.

for(let i = 0; i < datearg.length; i++) 
    {
        log.info(datearg.charAt(i));
        if(/[a-z]/.test(datearg.charAt(i))) letter_num++; letters.push(datearg.charAt(i));
        if(/[0-9]/.test(datearg.charAt(i))) number_num++; numbers.push(datearg.charAt(i));
    }

It seems like both if statements consistently return true, leading to all characters from datearg being included in the arrays. Can anyone help shed light on why this is happening?

Answer №1

if(/[a-z]/.test(datearg.charAt(i))) letter_num++; letters.push(datearg.charAt(i));

The code above can be rewritten as:

if(/[a-z]/.test(datearg.charAt(i))) { letter_num++; }
letters.push(datearg.charAt(i));

Essentially, the push method is not affected by the conditional statement. This lack of braces in control structures with a single statement is often discouraged in style guides for clarity and consistency.

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

Tips for properly implementing a bcrypt comparison within a promise?

Previously, my code was functioning correctly. However, it now seems to be broken for some unknown reason. I am using MariaDB as my database and attempting to compare passwords. Unfortunately, I keep encountering an error that says "Unexpected Identifier o ...

Error: An unhandled promise was rejected due to a TypeError: Attempting to access the 'length' property of undefined vertices in three.js

Currently in the process of transforming an html project into a laravel/vue setup, I stumbled upon a three.min.js file within the original html project. Unfortunately, the version of this file remains elusive to me along with some custom script files. To ...

A guide on seamlessly incorporating FlotJs functionalities into a ReactJs application

Having trouble integrating Flot charts into React due to a '$.plot' is not a function error. Here's the code I'm using: Script tags Index.html <script src="dist/libs/js/jquery.min.js"></script> <script src="dist/libs/js ...

Guide to conditionally adding a property to an object in JavaScript

After scouring both Stack Overflow and Google for a solution, I finally stumbled upon this brilliant idea: x = { y: (conditionY? 5 : undefined), z: (conditionZ? 5 : undefined), w: (conditionW? 5 : undefined), v: (conditionV? 5 : undefined), u ...

Managing multiple Socket.io connections upon page reload

I am currently developing a real-time application and utilizing Socket.io for its functionality. At the moment, my setup involves receiving user-posted messages through the socket server, saving this data to a MySQL database via the controller, and then b ...

Ant Design - Select - Automatically close dropdown when no data is available

I attempted setting notFoundContent to undefined, however it did not resolve the issue. Are there any other possible solutions for this problem? ...

The process of transferring information from a JSON API to TypeScript models

When working with my JSON API in my services, I need to pass the data to my models. What is the most efficient way to accomplish this task? Currently, I am following this process: import { Attachment } from '.'; export class Contact { id: nu ...

Select a hyperlink to access the corresponding tab

I've been playing around with bootstrap and AngularJS. I placed nav-tabs inside a modal-window and have it open when clicking on a link. However, I want the appropriate tab to open directly upon click. For example, if I click on "travel", I want the t ...

Unable to send email through Ajax/PHP contact form

It's quite amusing that it worked perfectly for one evening. After reaching out to my host, they assured me that there should be no issues with it not working. I even tried testing it in Firebug, but it appeared to be sending successfully. Additionall ...

After reaching the conclusion, the code restarts

Any ideas on how to reset this code every 10-20 seconds? I'm struggling to find a solution! I'm new to coding, so any assistance would be greatly appreciated. Here's the code: var items = document.getElementsByClassName('btn-primary n ...

storing data in local storage using JavaScript

As someone new to coding, my aim is to create a basic "daily planner" page where users can input text on different lines. I've been attempting to use local storage for this task, but seem to be running into some issues. I've experimented with get ...

Having trouble executing `npm start` command

After running npx create-react-app and then npm start, I encountered an error message https://i.sstatic.net/Uj5EC.png. Despite following the suggested solutions, the error persists. Any idea why this is happening? ...

Ways to observe redux action flow within a component

I am currently developing a React application structured with the following elements: redux typesafe-actions redux-observable My query is: How can I trigger a UI action when a specific redux action occurs? For instance, if we have these asynchronous ac ...

"Encountered an error with the code example for partitioning routes in Express.js

Note: For those who are new to this, there is an error in the Express documentation example. Make sure to include the next(); statement in your code like so: people.use(function(req, res, next) { next();//This line is necessary for the example to work ...

Tips for Returning Multiple Arrays from a JavaScript Function

This issue has been raised a few times before, but my situation is slightly different. I am new to JavaScript and have created this basic program to demonstrate the problem. var iterations = 0; function someFunc(x, y, z) { for (var i=0; i<4; i++) ...

Please follow the format of "M d, yy" when entering the date

I need to change the format of newDate from Tue Dec 24 2013 00:00:00 GMT+0530 to Dec 24, 2013 var dateString = 'Dec 17, 2013'; // date string var actualDate = new Date(dateString); // convert to actual date var newDate = new Date(actualDate.getF ...

Ways to update HTML content generated by Ajax

Having some difficulties modifying the HTML content generated by AJAX. This is the code snippet from the page: @model IEnumerable<WE_SRW_PeerNissen.Models.Reservations> @{ ViewBag.Title = "List"; Layout = "~/Views/Shared/_Layout.cshtml"; } ...

execute the function whenever the variable undergoes a change

<script> function updateVariable(value){ document.getElementById("demo").innerHTML=value; } </script> Script to update variable on click <?php $numbers=array(0,1,2,3,4,5); $count=sizeof($numbers); echo'<div class="navbox"> ...

SEO optimized jQuery plugin for embedding Twitter feeds

After extensive searching, I have been unable to locate a jQuery Twitter plugin that is SEO friendly. When I say SEO friendly, I am referring to one that first loads the data in HTML and then later uses AJAX. Many plugins load solely through AJAX, making ...

How can the value of a number in Angular be changed without altering its original value?

Imagine having the initial number 100. If I enter 50 in another input, it should add 50 to 100. However, if I then change the value from 50 to 80, the total should be 180 and not 230. The goal is always to add numbers to the original sum, not the new valu ...