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 201 of 801
Decimal count of a Number in JavaScript
Given there is a number with digits after decimal point and the task is to find the count of digits after the decimal point. Input Output Scenario Let's look into the input output scenario, where there is a floating point number having some digits after decimal point. Input = 45.36346323 Output = 8 As we can see in the above snippet there are 8 digits in the floating point number after the decimal point. To achieve this task, we'll explore three different methods using isInteger(), toString(), and split() methods. Method 1: Using ...
Read MoreConverting two arrays into a JSON object in JavaScript
Given two arrays and the task is to convert those two arrays into a JSON object. Where the first array elements are keys of the object and the second array elements are values of the object. Input-Output Scenario Let's look into the scenario of how two arrays will be converted into a JSON object. Consider two arrays are having some elements. Now, we need to convert these two arrays into a JSON object. Array1 = [1, 2, 3, 4]; Array2 = ['A', 'B', 'C', 'D']; Output = {"1":"A", "2":"B", "3":"C", "4":"D"} //JSON object ...
Read MoreUsing one array to help filter the other in JavaScript
When working with arrays of objects, you often need to filter one array based on values from another array. This is a common requirement when you have a list of items and want to keep only those that match specific criteria. Problem Statement Given an array of objects and an array of values, we need to filter the objects array to include only those objects whose property matches values in the second array. Sample Data Let's start with these arrays: const main = [ {name: "Karan", age: 34}, {name: "Aayush", ...
Read MoreFinding the immediate next character to a letter in string using JavaScript
We are required to write a JavaScript function that takes in a string of characters, str, and a single character, char. Our function should construct a new string that contains the immediate next character present in str after each instance of char (if any). Problem Given a string and a target letter, we need to find all characters that immediately follow each occurrence of the target letter and combine them into a new string. Solution We'll iterate through the string and check each character. When we find a match with our target letter, we'll add ...
Read MoreRemoving property from a JSON object in JavaScript
A JSON (JavaScript Object Notation) object is a data structure surrounded by curly braces {}. JSON objects contain key-value pairs where keys must be strings and values can be numbers, strings, objects, booleans, arrays, or null. JavaScript provides several methods to remove properties from objects. Each method has different characteristics - some modify the original object, while others create new objects without the unwanted properties. Basic JSON Object Structure Here's the basic syntax for a JSON object: const jsonObject = { "name": "John", "age": 30, "city": ...
Read MoreBest way to find length of JSON object in JavaScript
In JavaScript, objects don't have a built-in length property like arrays. However, there are several reliable methods to find the number of properties in a JSON object. Input-Output Scenario Consider an object with some keys and values. We need to get the count of its properties: const MyObj = { Name: "Mike", Age: 34 }; console.log(Object.keys(MyObj).length); // Output: 2 Using Object.keys() (Recommended) The Object.keys() method returns an array of a given object's own enumerable property names. It returns the keys in the same order as they ...
Read MoreCounting smaller and greater in JavaScript
In JavaScript, we often need to count elements in an array that are greater than or smaller than a specific value. This is useful for data analysis, filtering, and statistical operations. Let's say we have an array of numbers and want to count how many elements are greater than and smaller than a given number n. const arr = [3, 5, 5, 2, 23, 4, 7, 8, 8, 9]; console.log("Array:", arr); Array: [3, 5, 5, 2, 23, 4, 7, 8, 8, 9] Using reduce() Method The most elegant approach uses the ...
Read MoreMean of an array rounded down to nearest integer in JavaScript
Problem We need to write a JavaScript function that takes an array of numbers and returns the average (mean) of the array rounded down to the nearest integer using Math.floor(). Example Following is the code − const arr = [45, 23, 67, 68, 12, 56, 99]; const roundedMean = (arr = []) => { const { sum, count } = arr.reduce((acc, val) => { let { sum, count } = acc; count++; ...
Read MoreIs element repeated more than n times in JavaScript
In JavaScript, you may need to check if any element appears more than a specified number of times in an array. This is useful for data validation, duplicate detection, and enforcing constraints on array contents. Problem Definition We need to write a function that takes two arguments: An Array of literals that may contain repeating elements A number representing the maximum allowed occurrences (limit) The function should return false if any element appears more than the limit, and true otherwise. Using reduce() and every() Methods The ...
Read MoreConverting decimal to binary or hex based on a condition in JavaScript
Problem We need to write a JavaScript function that takes in a number n and converts it based on a condition: If a number is even, convert it to binary. If a number is odd, convert it to hex. Solution We can use JavaScript's toString() method with different radix values to perform the conversion. For binary conversion, we use radix 2, and for hexadecimal conversion, we use radix 16. Example Here's the implementation: const num = 1457; const conditionalConvert = (num = 1) ...
Read More