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 16 of 589
Complete Equation by Filling Missing Operator in JavaScript
We are required to write a JavaScript function that takes in a bunch of numbers and returns the correct sequence of operations to satisfy the equation. The operators that can be used are (+, −, *, /, ^, %). For example − Input : 5 3 8 Output : 5+3=8 Input : 9 27 3 Output : 9=27/3 Input : 5 2 25 , 1 5 2 Output : 5^2=25 , 1=5%2 For each input, there is at ...
Read MoreSort an array to have specific items first in the array - JavaScript
When working with arrays of objects, you might need to sort them so that items with specific properties appear first. This is a common requirement in applications where you need to prioritize certain elements while maintaining the original order of others. Suppose we have an array of objects like this: const arr = [ {flag: true, other: 1}, {flag: true, other: 2}, {flag: false, other: 3}, {flag: true, other: 4}, {flag: true, other: 5}, ...
Read MoreHow to find a nearest higher number from a specific set of numbers: JavaScript ?
We have a set of numbers and our requirement is to find the same or the nearest higher number key to a specific number provided as the input to the function. The set of numbers is defined as: const numbers = { A: 107, B: 112, C: 117, D: 127, E: 132, F: 140, G: 117, H: 127, I: 132, J: 132, K: 140, ...
Read MoreMerge two objects in JavaScript ignoring undefined values
When merging two objects in JavaScript, you may want to ignore undefined values to avoid overwriting valid data. The spread operator alone doesn't handle this logic, so we need a custom approach. The Problem Consider these two objects where some properties have undefined values: const A = { activity: 'purchased', count: undefined, time: '09:05:33' }; const B = { activity: 'purchased', count: '51', time: undefined }; // Simple spread operator overwrites values const simpleSpread = { ...A, ...B }; console.log("Simple spread:", simpleSpread); Simple spread: { activity: 'purchased', count: '51', time: undefined } ...
Read MoreSplitting an object into an array of objects in JavaScript
Splitting an object into an array of separate objects is useful when you need to transform key-value pairs into individual objects for easier manipulation or iteration. Suppose we have an object like this: const obj = { "name": "John", "age": 30, "city": "New York", "profession": "Developer" }; We need to write a JavaScript function that takes such an object and returns a new array where each key-value pair becomes its own separate object. Using Object.keys() and forEach() const obj = ...
Read MoreConvert array into array of subarrays - JavaScript
We are required to write a JavaScript function that takes in an array of literals and returns a new array that have elements of the original array chunked into subarrays of length exactly 2. Now if the length of original array is not exactly divisible by 2, then the last subarray should have only one element. For example, if the input array is: const arr = [1, 2, 3, 4, 5, 6, 7]; Then the output should be: const output = [[1, 2], [3, 4], [5, 6], [7]] Using Loop-based ...
Read MoreEvaluating a string as a mathematical expression in JavaScript
We are required to write a JavaScript function that takes in a stringified mathematical equation. The function should return the result of the equation provided to the function. For example: If the equation is − const str = '1+23+4+5-30'; Then the output should be 3 Example The code for this will be − const str = '1+23+4+5-30'; const compute = (str = '') => { let total = 0; str = str.match(/[+\-]*(\.\d+|\d+(\.\d+)?)/g) || []; while (str.length) { ...
Read MoreJavaScript Algorithm - Removing Negatives from the Array
Given an array X of multiple values (e.g. [-3, 5, 1, 3, 2, 10]), we need to write a function that removes any negative values from the array using only the pop() method without creating temporary arrays. The challenge is to modify the original array in-place, maintaining only positive numbers while using just the pop() method for removal. Algorithm Approach The algorithm works in two phases: Remove all negative values from the end of the array For remaining negatives, replace them with the last positive element and pop Complete Implementation function ...
Read MoreJavaScript: Combine highest key values of multiple arrays into a single array
We are required to write a JavaScript function that takes in any number of arrays of numbers. Our function should return an array of greatest numbers picked from the corresponding positions of the input arrays. The number of elements in the output array should be equal to the length of the longest input array. Problem Understanding Given multiple arrays, we need to compare elements at the same index positions and select the maximum value from each position to form a new array. Example The code for this will be: const arr1 = [117, 121, ...
Read MoreHow to join JavaScript array of string
We need to write a JavaScript function that joins an array of strings, replaces all whitespaces with dashes "-", and returns the formatted string. For example: If the array is − const arr = ["QA testing promotion ", " Twitter ", "Facebook ", "Test"]; Then the output should be − const output = "QA-testing-promotion-Twitter-Facebook-Test"; Method 1: Using Loop and String Concatenation This approach joins the array elements and processes each character to replace spaces with dashes: const arr = ["QA testing promotion ", " Twitter ", "Facebook ", ...
Read More