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 473 of 801
Extract properties from an object in JavaScript
We have to write a JavaScript function, say extract() that extracts properties from an object to another object and then deletes them from the original object. For example − If obj1 and obj2 are two objects, then obj1 = {color:"red", age:"23", name:"cindy"} obj2 = extract(obj1, ["color", "name"]) After passing through the extract function, they should become like − obj1 = { age:23 } obj2 = {color:"red", name:"cindy"} Therefore, let's write the code for this function − Syntax const extract = (obj, ...keys) => { ...
Read MoreConvert an array of binary numbers to corresponding integer in JavaScript
Let's say, we have an array of Numbers that contains 0 and 1 − const arr = [0, 1, 0, 1]; We are required to write an Array function, toBinary() that returns the corresponding decimal integer for the array it is used with. For example − If the array is − const arr = [1, 0, 1, 1]; Then the output should be 11 because the decimal representation of binary 1011 is 11. Therefore, let's write the code for this function. Method 1: Using parseInt() with join() In ...
Read MoreCounting the number of redundant characters in a string - JavaScript
We are required to write a JavaScript function that takes in a string and returns the count of redundant characters in the string. A redundant character is any character that appears more than once, where all duplicate occurrences (except the last one) are considered redundant. For example − If the string is − const str = 'abcde' Then the output should be 0 because each character appears only once. If the string is − const str = 'aaacbfsc'; Then the output should be 3 because 'a' appears 3 times (2 ...
Read MoreAggregate records in JavaScript
Aggregating records in JavaScript involves combining multiple objects with the same identifier into a single object with accumulated values. This is commonly used for data analysis tasks like summing transactions by person or grouping sales by product. Let's say we have an array of objects that contains information about some random transactions carried out by some people: const transactions = [{ name: 'Rakesh', amount: 1500 }, { name: 'Rajesh', amount: 1200 }, { name: 'Ramesh', ...
Read MoreSorting digits of all the number of array - JavaScript
We are required to write a JavaScript function that takes in an array of numbers and reorders the digits of all the numbers internally in a specific order (let's say in ascending order for the sake of this problem). For example − If the array is − const arr = [543, 65, 343, 75, 567, 878, 87]; Then the output should be − const output = [345, 56, 334, 57, 567, 788, 78]; Approach The solution involves converting each number to a string, splitting it into individual digits, sorting those ...
Read MoreReorder array based on condition in JavaScript?
Let's say we have an array of objects that contains the scores of some players in a card game: const scorecard = [{ name: "Zahir", score: 23 }, { name: "Kabir", score: 13 }, { name: "Kunal", score: 29 }, { name: "Arnav", score: 42 }, { name: "Harman", score: 19 }, { name: ...
Read MoreJoining two strings with two words at a time - JavaScript
We are required to write a JavaScript function that takes in two strings, creates and returns a new string with first two characters of first string, next two characters of second string, then first, then second and so on. For example: If the strings are: const str1 = 'Hello world'; const str2 = 'How are you btw'; Then the output should be: 'HeHollw o arwoe rlyodu btw' Approach The algorithm alternates between the two strings, taking two characters at a time. When one string is exhausted, it appends the ...
Read MoreCount unique elements in array without sorting JavaScript
When working with arrays containing duplicate values, counting unique elements is a common task. Let's explore different approaches to count unique elements in an array without sorting it first. Consider this sample array with duplicate values: const arr = ['Cat', 'Dog', 'Cat', 'Elephant', 'Dog', 'Grapes', 'Dog', 'Lion', 'Grapes', 'Lion']; console.log('Original array:', arr); Original array: ['Cat', 'Dog', 'Cat', 'Elephant', 'Dog', 'Grapes', 'Dog', 'Lion', 'Grapes', 'Lion'] Using Array.reduce() and lastIndexOf() This approach uses lastIndexOf() to identify the last occurrence of each element, ensuring each unique value is counted only once: const ...
Read MoreConverting a proper fraction to mixed fraction - JavaScript
A proper fraction is one where the numerator is smaller than the denominator, represented in p/q form where both p and q are natural numbers. What is a Mixed Fraction? When we divide the numerator (a) of a fraction by its denominator (b), we get a quotient (q) and remainder (r). The mixed fraction form for fraction a/b is: Mixed form: q r b This is pronounced as "q wholes and r by b". Problem Statement We need to write a ...
Read MoreTop n max value from array of object JavaScript
Finding the top n objects with maximum values from an array is a common programming task. Let's explore how to get objects with the highest duration values from an array of objects. Problem Statement Given an array of objects, we need to create a function that returns the top n objects based on the highest duration values. const arr = [ {"id":0, "start":0, "duration":117, "slide":4, "view":0}, {"id":0, "start":0, "duration":12, "slide":1, "view":0}, {"id":0, "start":0, "duration":41, "slide":2, "view":0}, {"id":0, "start":0, "duration":29, "slide":3, ...
Read More