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
Object Oriented Programming Articles
Page 24 of 589
Sorting numbers in descending order but with `0`s at the start JavaScript
We are required to write a JavaScript function that takes in an array of numbers. The function should sort the array of numbers on the following criteria: If the array contains any zeros, they should all appear in the beginning. All the remaining numbers should be placed in a decreasing order. For example, if the input array is: const arr = [4, 7, 0, 3, 5, 1, 0]; Then after applying the sort, the array should become: const output = [0, 0, 7, 5, ...
Read MoreHow to convert array of decimal strings to array of integer strings without decimal in JavaScript
We are required to write a JavaScript function that takes in an array of decimal strings. The function should return an array of strings of integers obtained by flooring the original corresponding decimal values of the array. For example, If the input array is − const input = ["1.00", "-2.5", "5.33333", "8.984563"]; Then the output should be − const output = ["1", "-2", "5", "8"]; Method 1: Using parseInt() The parseInt() function parses a string and returns an integer. It automatically truncates decimal parts. const input = ["1.00", ...
Read MoreCan form target array from source array JavaScript
We are given an array of distinct integers, let's say arr, and another array of integer arrays, let say sourceArr. In the sourceArr array, the integers are distinct. We should write a function that forms arr by concatenating the arrays in sourceArr in any order. However, we cannot reorder the integers inside of any subarray in the sourceArr. We should return true if it is possible to form the array arr from sourceArr, false otherwise. For example: const arr = [23, 67, 789]; const sourceArr = [[23], [789, 67]]; The function should return ...
Read MoreHow to convert array into array of objects using map() and reduce() in JavaScript
Converting arrays into arrays of objects is a common task in JavaScript. This article shows how to use map() and reduce() together to transform nested arrays into structured objects. Problem Statement Suppose we have an array of arrays like this: const arr = [ [ ['juice', 'apple'], ['maker', 'motts'], ['price', 12] ], [ ['juice', 'orange'], ['maker', 'sunkist'], ['price', 11] ] ]; We need to convert this nested array structure into ...
Read MoreSum of all multiples in JavaScript
We need to write a JavaScript function that takes a number n as the first argument, followed by any number of divisor arguments. The function should sum all numbers up to n that are divisible by any of the specified divisors. Problem Statement Given a number n and multiple divisors, find the sum of all numbers from 1 to n that are multiples of any of the divisors. For example, if we call: sumMultiples(15, 2, 3) We need to find numbers up to 15 that are divisible by either 2 or 3: ...
Read MoreGenerate Ranking with combination of strings in JavaScript
We are required to write a JavaScript function that takes in any number of arrays of numbers and generates a frequency map of all possible combinations within each array. The function counts how many times each element and combination appears across all arrays. For example, if we have the following arrays: const a = [23, 45, 21], b = [45, 23], c = [21, 32], d = [23], e = [32], f = [50, 54]; The function should ...
Read MoreReversing the order of words of a string in JavaScript
We are required to write a JavaScript function that takes in a string as the only argument. The function should reverse the order of the words in the string and return the new string. The only condition is that we cannot use the inbuilt array method reverse(). For example − If the input string is − const str = 'this is a string'; Then the output string should be − string a is this Using Manual Array Reversal We can split the string into words, manually reverse ...
Read MoreAll combinations of sums for array in JavaScript
We are required to write a JavaScript function that takes in an array of Numbers as the first argument and a number, say n as the second argument. The number n will always be less than or equal to the length of the array. Our function should return an array of the sum of all elements of all the possible subarrays of length n from the original array. For example, If the input is: const arr = [2, 6, 4]; const n = 2; Then the output should be: const output = ...
Read MoreAwkward behaviour of delete operator on arrays in JavaScript
The delete operator in JavaScript is actually an object operator (used with objects). But since arrays are also indexed objects in JavaScript, we can use the delete operator with arrays as well. However, this leads to some awkward behavior that can confuse developers. Consider the following array of literals: const arr = ['a', 'b', 'c', 'd', 'e']; Example Let us now execute the following program and observe the unexpected output: const arr = ['a', 'b', 'c', 'd', 'e']; delete arr[4]; console.log(arr); console.log(arr.length); Output [ 'a', 'b', 'c', ...
Read MoreDynamic Programming: return all matched data in JavaScript
Dynamic programming in JavaScript can be used to efficiently search and retrieve data from nested structures. This article demonstrates how to search for all matching cities in a complex JSON object containing country and province information. The Data Structure We have a nested JSON object with countries, provinces, and cities: const countryInfo = { country: [{ name: "Bangladesh", province: [{ name:"Dhaka", ...
Read More