Object Oriented Programming Articles

Page 98 of 589

How do I search through an array using a string, which is split into an array with JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 276 Views

We are given an array of strings and another string for which we are required to search in the array. We can filter the array checking whether it contains all the characters that user provided through the input. Using Array Filter with Split (Flexible Search) This approach splits the search string into parts and checks if each part exists in the array elements, regardless of order. const deliveries = ["14/02/2020, 11:47, G12, Kalkaji", "13/02/2020, 11:48, A59, Amar Colony"]; const input = "g12, kal"; const pn = input.split(" "); const requiredDeliveries = deliveries.filter(delivery => ...

Read More

Is there any way I can call the validate() function outside the initValidation() function in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 218 Views

When you define a function inside another function in JavaScript, it's only accessible within the parent function's scope. To call the validate() function outside of initValidation(), you have several approaches. The Problem In the following code, validate() is scoped inside initValidation() and cannot be accessed externally: function initValidation(){ // irrelevant code here function validate(_block){ // code here } } Method 1: Using Constructor Pattern Convert the parent function into a constructor and assign ...

Read More

Join every element of an array with a specific character using for loop in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 330 Views

In JavaScript, you can join array elements with a specific character using a for loop. This approach gives you fine control over how elements are combined and what separators or wrappers to use. Problem Statement We need to create a function that takes an array and a string, then returns a new string where each array element is wrapped by the given string. For example: applyText([1, 2, 3, 4], 'a') should return 'a1a2a3a4a' Using for Loop Approach Here's how to solve this using a for loop to iterate through the array: ...

Read More

Replace() with Split() in JavaScript to append 0 if number after comma is a single digit

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 345 Views

When working with decimal numbers as strings, you often need to format them properly. This article shows how to append a zero to single-digit numbers after a comma using JavaScript's split() and replace() methods. Problem Statement Given a string like "250, 5", we need to: Append a zero if the number after the comma is a single digit Return -1 if the string contains more than one comma Solution Using split() and replace() We can combine split() and replace() methods to achieve this: const a = "250, 5"; const roundString = (str) ...

Read More

Get filename from string path in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 2K+ Views

We need to write a function that takes in a string file path and returns the filename. Filename usually lives right at the very end of any path, although we can solve this problem using regex but there exists a simpler one-line solution to it using the string split() method of JavaScript and we will use the same here. Let's say our file path is − "/app/base/controllers/filename.js" Using split() Method Following is the code to get file name from string path − const filePath = "/app/base/controllers/filename.js"; const extractFilename = (path) => { ...

Read More

Combine objects and delete a property with JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 262 Views

We have the following array of objects that contains two objects and we are required to combine both objects into one and get rid of the chk property altogether: const err = [ { "chk" : true, "name": "test" }, { "chk" : true, "post": "test" } ]; ...

Read More

How to create a function which returns only even numbers in JavaScript array?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

Here, we need to write a function that takes one argument, which is an array of numbers, and returns an array that contains only the numbers from the input array that are even. So, let's name the function as returnEvenArray, the code for the function will be − Example const arr = [3, 5, 6, 7, 8, 4, 2, 1, 66, 77]; const returnEvenArray = (arr) => { return arr.filter(el => { return el % 2 === 0; }) }; ...

Read More

Strange syntax, what does `?.` mean in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 2K+ Views

The ?. operator is called optional chaining and provides a safe way to access nested object properties without throwing errors when intermediate properties are undefined or null. The Problem with Regular Property Access Consider this nested object describing a person: const being = { human: { male: { age: 23 } } }; console.log(being.human.male.age); 23 This works fine, but what happens if we try to access a property that doesn't exist? ...

Read More

What is this problem in JavaScript's selfexecuting anonymous function?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 223 Views

Let's analyze this JavaScript code snippet that demonstrates a common confusion with variable hoisting and function declarations in self-executing anonymous functions. var name = 'Zakir'; (() => { name = 'Rahul'; return; console.log(name); function name(){ let lastName = 'Singh'; } })(); console.log(name); Naive Analysis (Incorrect) At first glance, you might expect this sequence: Global variable name is set to 'Zakir' Inside the IIFE, name is ...

Read More

How to print star pattern in JavaScript in a very simple manner?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

Here is a simple star pattern that we are required to print inside the JavaScript console. Note that it has to be printed inside the console and not in the output or HTML window: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Here's the code for doing so in JavaScript: Example const star = "* "; // where length is no of stars in longest streak const length = 6; for(let i = 1; i

Read More
Showing 971–980 of 5,881 articles
« Prev 1 96 97 98 99 100 589 Next »
Advertisements