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 146 of 589
Shift last given number of elements to front of array JavaScript
In JavaScript, moving elements from the end of an array to the front is a common array manipulation task. This tutorial shows how to create a function that shifts the last n elements to the beginning of an array in-place. Problem Statement We need to create an array method reshuffle() that: Takes a number n (where n ≤ array length) Moves the last n elements to the front Modifies the array in-place Returns true on success, false if n exceeds array length Example Input and Output // Original array const arr = ["blue", ...
Read MoreConvert buffer to readable string in JavaScript?
In Node.js, buffers store binary data and need to be converted to strings for readability. The toString() method with encoding parameters handles this conversion. What is a Buffer? A Buffer is a Node.js object that represents a fixed-size chunk of memory allocated outside the JavaScript heap. It's used to handle binary data directly. Basic Buffer to String Conversion Use the toString() method with the appropriate encoding. UTF-8 is the most common encoding for text data. // Create a buffer from string var actualBufferObject = Buffer.from('[John Smith]', 'utf8'); console.log("The actual buffer object:"); console.log(JSON.stringify(actualBufferObject)); // ...
Read MoreArray sum: Comparing recursion vs for loop vs ES6 methods in JavaScript
When working with arrays in JavaScript, there are multiple ways to calculate the sum of all elements. Let's compare three popular approaches: recursion, traditional for loops, and ES6 methods like reduce(). To demonstrate performance differences, we'll test each method with a large number of iterations. This gives us insights into which approach performs best for array summation tasks. Recursive Approach The recursive method calls itself until it processes all array elements: const recursiveSum = (arr, len = 0, sum = 0) => { if(len < arr.length){ ...
Read MoreJavaScript outsider function call and return the result
In JavaScript, you can call functions from outside their defining scope by using the return keyword to return inner functions. This creates closures that maintain access to outer variables. Syntax function outerFunction() { // Outer variables var outerVar = value; // Inner function var innerFunction = function() { // Can access outerVar return result; } ...
Read MoreCompare two objects in JavaScript and return a number between 0 and 100 representing the percentage of similarity
When comparing objects in JavaScript, we often need to determine their similarity as a percentage. This is useful for data matching, filtering, or recommendation systems. Problem Overview Given two objects, we need to calculate their similarity percentage based on matching key-value pairs. The similarity is calculated by dividing the count of matching properties by the total properties in the smaller object. const a = { Make: "Apple", Model: "iPad", hasScreen: "yes", Review: "Great product!", }; const b = { ...
Read MoreHow Do I Find the Largest Number in a 3D JavaScript Array?
To find the largest number in a 3D JavaScript array, we can use flat(Infinity) to flatten all nested levels, then apply Math.max() to find the maximum value. The Problem 3D arrays contain multiple levels of nested arrays, making it difficult to directly compare all numbers. We need to flatten the structure first. var theValuesIn3DArray = [75, [18, 89], [56, [97], [99]]]; Using flat() with Math.max() The flat(Infinity) method flattens all nested levels, and the spread operator ... passes individual elements to Math.max(). var theValuesIn3DArray = [75, [18, 89], [56, [97], [99]]]; ...
Read MoreSolve the Sherlock and Array problem in JavaScript
Watson gives Sherlock an array A of length N. Then he asks him to determine if there exists an element in the array such that the sum of the elements on its left is equal to the sum of the elements on its right. We have to write this function, it should take in an array of Numbers, and any such number exists in the array, it should return its index, otherwise it should return -1. So, let's write the code for this function. Algorithm Approach The efficient approach is to: Calculate the total sum of ...
Read MoreChecking for co-prime numbers - JavaScript
Two numbers are said to be co-primes if there exists no common prime factor amongst them (1 is not a prime number). For example: 4 and 5 are co-primes 9 and 14 are co-primes 18 and 35 are co-primes 21 and 57 are not co-prime because they have 3 as the common prime factor We are required to write a function that takes in two numbers and returns true if they are co-primes otherwise returns false. Understanding Co-prime Numbers Two numbers are co-prime if their greatest common divisor (GCD) is 1. This means ...
Read MoreHow to force JavaScript to do math instead of putting two strings together?
JavaScript often concatenates when you expect addition because the + operator works differently with strings and numbers. Here are several methods to force mathematical addition instead of string concatenation. The Problem When JavaScript encounters the + operator with strings, it performs concatenation instead of addition: console.log("3" + "5"); // "35" (concatenation) console.log(3 + 5); // 8 (addition) console.log("3" + 5); // "35" (string wins, concatenation) 35 8 35 Using the Unary Plus Operator ...
Read MoreFind even odd index digit difference - JavaScript
We are required to write a JavaScript function that takes in a number and returns the difference of the sums of the digits at even place and the digits at odd place. Understanding the Problem Given a number, we need to: Sum all digits at even positions (0, 2, 4, ...) Sum all digits at odd positions (1, 3, 5, ...) Return the absolute difference between these sums Example For the number 345336: Position 0: 6 (even) Position 1: 3 (odd) Position 2: 3 (even) Position 3: 5 (odd) Position 4: 4 (even) ...
Read More