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 135 of 589
Prefix sums (Creating an array with increasing sum) with Recursion in JavaScript
Consider the following array of numbers: const arr = [10, 5, 6, 12, 7, 1]; The sum of its consecutive elements taking one less element in every go will be: [10, 5, 6, 12, 7, 1] = 10 + 5 + 6 + 12 + 7 + 1 = 41; [5, 6, 12, 7, 1] = 5 + 6 + 12 + 7 + 1 = 31; [6, 12, 7, 1] = 6 + 12 + 7 + 1 = 26; [12, 7, 1] = 12 + 7 + 1 = 20; [7, 1] ...
Read MoreRemove extra spaces in string JavaScript?
To remove extra spaces from strings in JavaScript, you can use several methods depending on your needs. Here are the most common approaches. Remove All Spaces To remove all spaces from a string, use the replace() method with a regular expression: var sentence = "My name is John Smith "; console.log("Original string:"); console.log(sentence); var noSpaces = sentence.replace(/\s+/g, ''); console.log("After removing all spaces:"); console.log(noSpaces); Original string: My name is John Smith After removing all spaces: MynameisJohnSmith Remove Leading and Trailing Spaces Use trim() to remove spaces only from the ...
Read MoreDetermining happy numbers using recursion JavaScript
A happy number is a number which eventually reaches 1 when replaced by the sum of the square of each digit. Whereas if during this process any number gets repeated, the cycle will run infinitely and such numbers are called unhappy numbers. For example − 13 is a happy number because, 1^2 + 3^2 = 10 and, 1^2 + 0^2 = 1 On the other hand, 36 is an unhappy number. We are required to write a function that uses recursion to determine whether or not a number is a happy number. Algorithm ...
Read MoreRemove characters from a string contained in another string with JavaScript?
When working with strings in JavaScript, you might need to remove all characters from one string that appear in another string. This can be achieved using the replace() method combined with reduce(). Problem Statement Given two strings, we want to remove all characters from the first string that exist in the second string: var originalName = "JOHNDOE"; var removalName = "JOHN"; // Expected result: "DOE" Solution Using replace() and reduce() The reduce() method iterates through each character in the removal string, and replace() removes the first occurrence of that character from the original ...
Read MoreMap an integer from decimal base to hexadecimal with custom mapping JavaScript
Usually when we convert a decimal to hexadecimal (base 16) we use the set 0123456789ABCDEF to map the number. We are required to write a function that does exactly the same but provides user the freedom to use any scale rather than the one mentioned above. For example − The hexadecimal notation of the decimal 363 is 16B But if the user decides to use, say, a scale 'qwertyuiopasdfgh' instead of '0123456789ABCDEF', the number 363, then will be represented by wus That's what we are required to do. So, let's do this by ...
Read MoreReturn Largest Numbers in Arrays passed using reduce method?
To find the largest number in each array using the reduce() method, combine it with Math.max() and the spread operator. This approach processes multiple arrays and returns an array of maximum values. Syntax array.reduce((accumulator, currentArray) => { accumulator.push(Math.max(...currentArray)); return accumulator; }, []); Example const getBiggestNumberFromArraysPassed = allArrays => allArrays.reduce( (maxValue, maxCurrent) => { maxValue.push(Math.max(...maxCurrent)); return maxValue; }, [] ); console.log(getBiggestNumberFromArraysPassed([[45, ...
Read MoreFind longest string in array (excluding spaces) JavaScript
We are required to write a function that accepts an array of string literals and returns the index of the longest string in the array. While calculating the length of strings we don't have to consider the length occupied by whitespaces. If two or more strings have the same longest length, we have to return the index of the first string that does so. We will iterate over the array, split each element by whitespace, join again and calculate the length. We'll track the maximum length and its index, updating when we find a longer string. Syntax ...
Read MoreAssign new value to item in an array if it matches another item without looping in JavaScript?
In JavaScript, you can assign a new value to an array item that matches a condition without using traditional loops. This can be achieved using array methods like filter() combined with map(), or more efficiently with forEach() or find(). Using filter() and map() The filter() method finds matching items, and map() modifies them. Here's how to change "Bob" to "Carol" in an array of objects: const studentDetails = [ {Name: "John"}, {Name: "David"}, {Name: "Bob"}, {Name: "Mike"} ]; var ...
Read MoreSort array by year and month JavaScript
Sorting arrays of objects by multiple criteria is a common requirement in JavaScript applications. In this tutorial, we'll learn how to sort an array by year first, then by month for objects with the same year. The Problem We have an array of objects containing year and month properties: const arr = [{ year: 2020, month: 'January' }, { year: 2017, month: 'March' }, { year: 2010, month: 'January' }, { ...
Read MoreCopy objects with Object.assign() in JavaScript
The Object.assign() method is used to copy one or more source objects to a target object. It invokes getters and setters since it uses both 'get' on the source and 'set' on the target. Syntax Object.assign(target, ...sourceObjects); The first parameter is the target object that will be modified and returned. The remaining parameters are source objects whose properties will be copied to the target. Basic Example const target = { a: 1, b: 2 }; const source = { b: 3, c: 4 }; const result = Object.assign(target, source); console.log("Target ...
Read More