Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Web Development Articles
Page 468 of 801
Product of two number using HOC - JavaScript
Higher Order Functions (HOC) in JavaScript are functions that either receive another function as an argument or return a function as their result. When combined with closures, HOCs become a powerful tool for creating reusable and modular code. In this example, we'll create a Higher Order Function that calculates the product of two numbers using the concept of currying, where a function returns another function. Example Here's how to implement a product calculator using HOC: const num1 = 24; const num2 = 5; const productHOC = num1 => { return ...
Read MoreJavaScript Match between 2 arrays
When working with two arrays in JavaScript, you often need to match elements from one array with corresponding elements in another. This article demonstrates how to extract user IDs from an object array based on a names array, maintaining the order of the names array. Problem Statement We have two arrays: one containing user objects with names and UIDs, and another containing just names. Our goal is to create a function that returns UIDs in the same order as they appear in the names array. const data = [{ name: 'Kamlesh Kapasi', ...
Read MoreChecking Oddish and Evenish numbers - JavaScript
A number is Oddish if the sum of all of its digits is odd, and a number is Evenish if the sum of all of its digits is even. We need to write a function that determines whether a number is Oddish or Evenish. The function should return true for Oddish values and false for Evenish values. How It Works The algorithm extracts each digit from the number, adds them up, and checks if the sum is odd or even: Extract digits using modulo (num % 10) and integer division (Math.floor(num / 10)) Sum all ...
Read MoreSort array of points by ascending distance from a given point JavaScript
Let's say, we have an array of objects with each object having exactly two properties, x and y that represent the coordinates of a point. We have to write a function that takes in this array and an object with x and y coordinates of a point and we have to sort the points (objects) in the array according to the distance from the given point (nearest to farthest). The Distance Formula It is a mathematical formula that states that the shortest distance between two points (x1, y1) and (x2, y2) in a two-dimensional plane is given by ...
Read MoreNeutralisation of strings - JavaScript
In JavaScript, string neutralisation involves evaluating a string containing only '+' and '-' characters to determine the overall sign. The concept is based on mathematical sign multiplication: like signs produce '+', unlike signs produce '-'. How Neutralisation Works The neutralisation follows these rules: ++ results in + (positive × positive = positive) -- results in + (negative × negative = positive) +- or -+ results in - (positive × negative = negative) Example String Let's work with the following string: const str = '+++-+-++---+-+--+-'; Implementation Here's how to implement ...
Read MoreHow to calculate the average in JavaScript of the given properties in the array of objects
We have an array of objects. Each object contains a few properties and one of these properties is age: const people = [ { name: 'Anna', age: 22 }, { name: 'Tom', age: 34 }, { name: 'John', ...
Read MoreReverse only the odd length words - JavaScript
We are required to write a JavaScript function that takes in a string and reverses the words in the string that have an odd number of characters in them. Any substring in the string qualifies to be a word, if either it is encapsulated by two spaces on either ends or present at the end or beginning and followed or preceded by a space. Let's say the following is our string: const str = 'hello beautiful people'; The odd length words are: hello (5 characters - odd) beautiful (9 characters - odd) ...
Read MoreHow to get odd and even position characters from a string?
In JavaScript, you can extract characters from odd and even positions in a string using various methods. This technique is useful for string manipulation tasks like creating puzzles, encoding, or data processing. Understanding the Problem Given a string, we want to separate characters based on their position index: Even positions (0, 2, 4...): "T", "i", " ", "s", " ", "a", " ", "e", "t", "!" Odd positions (1, 3, 5...): "h", "s", "i", "", "i", "", "t", "s", "" If the string is "This is a test!" Even positions: "Ti s a ...
Read MoreReturn index of first repeating character in a string - JavaScript
We are required to write a JavaScript function that takes in a string and returns the index of first character that appears twice in the string. If there is no such character then we should return -1. Following is our string: const str = 'Hello world, how are you'; Example Following is the code: const str = 'Hello world, how are you'; const firstRepeating = str => { const map = new Map(); for(let i = 0; i < str.length; i++){ ...
Read MoreTaking part from array of numbers by percent JavaScript
We have an array of number literals like this: const numbers = [10, 6200, 20, 20, 350, 900, 26, 78, 888, 10000, 78, 15000, 200, 1280, 2000, 450]; We need to write a function that takes an array of numbers and a percentage (0-100). The function should return the first n elements from the array that sum up to equal or just less than the specified percentage of the total array sum. Understanding the Problem Let's take a simpler example: const numbers = [12, 10, 6, 8, 4, 2, 8]; ...
Read More