Object Oriented Programming Articles

Page 29 of 589

Reversing the even length words of a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 407 Views

We are required to write a JavaScript function that takes in a string and reverses the words in the string that have an even number of characters in them. Let's say the following is our string: const str = 'This is an example string'; We want to reverse the even length words of the above string i.e. reverse the following words: This (4 characters) is (2 characters) an (2 characters) string (6 characters) The word "example" has 7 characters (odd length), so it remains unchanged. Syntax ...

Read More

Differences in two strings in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 513 Views

We are required to write a JavaScript function that takes in two strings and find the number of corresponding dissimilarities in the strings. The corresponding elements will be dissimilar if they are not equal at the same position. Example Strings Let's say the following are our strings: const str1 = 'Hello world!!!'; const str2 = 'Hellp world111'; In this case, we need to compare each character at the same index and count how many positions have different characters. Method 1: Using a For Loop const str1 = 'Hello world!!!'; const str2 ...

Read More

Finding the index of the first repeating character in a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 639 Views

We are required to write a JavaScript function that takes in a string and returns the index of the first character that appears twice in the string. If there is no such character then we should return -1. Let's say the following is our string − const str = 'Hello world, how are you'; We need to find the index of the first repeating character. Understanding the Problem In the string "Hello world, how are you", we need to find which character repeats first. The character 'l' appears at index 2 and again ...

Read More

Column sum of elements of 2-D arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 2K+ Views

We have an array of arrays and are required to write a function that takes in this array and returns a new array that represents the sum of corresponding elements of original array. If the original array is: [ [43, 2, 21], [1, 2, 4, 54], [5, 84, 2], [11, 5, 3, 1] ] Then the output should be: [60, 93, 30, 55] This means we add all elements at index 0 (43+1+5+11=60), all elements at index 1 (2+2+84+5=93), ...

Read More

Finding smallest number that satisfies some conditions in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 261 Views

We are required to write a JavaScript function that takes in a number as the first argument, say n, and an array of numbers as the second argument. The function should return the smallest n-digit number which is a multiple of all the elements specified in the array. If there exist no such n-digit element then we should return the smallest such element. For example: If the array is − const arr = [12, 4, 5, 10, 9] For both n = 2 and n = 3, we need to find the smallest number ...

Read More

Finding the inclination of arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 164 Views

We are required to write a JavaScript function that takes in an array of numbers and returns true if it's either strictly increasing or strictly decreasing, otherwise returns false. In Mathematics, a strictly increasing function is that function in which the value to be plotted always increases. Similarly, a strictly decreasing function is that function in which the value to be plotted always decreases. Understanding the Concept An array has a consistent inclination when: Strictly increasing: Each element is greater than the previous one Strictly decreasing: Each element ...

Read More

Check if the elements of the array can be rearranged to form a sequence of numbers or not in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 180 Views

We are required to write a JavaScript function that takes in an array of numbers and checks if the elements of the array can be rearranged to form a sequence of numbers or not. For example: If the array is − const arr = [3, 1, 4, 2, 5]; Then the output should be true because these numbers can be rearranged to form the consecutive sequence: 1, 2, 3, 4, 5. Approach To solve this problem, we need to: Sort the array in ascending order Check if each element is exactly ...

Read More

Finding the continuity of two arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 200 Views

We are required to write a JavaScript function that takes in two arrays of numbers. The function should return true if the two arrays upon combining can form a consecutive sequence, false otherwise. For example: If the arrays are − const arr1 = [4, 6, 2, 9, 3]; const arr2 = [1, 5, 8, 7]; When combined and sorted, these arrays form [1, 2, 3, 4, 5, 6, 7, 8, 9], which is a consecutive sequence. Therefore, the output should be true. Understanding Consecutive Sequences A consecutive sequence means each number is exactly ...

Read More

Performing the subtraction operation without the subtraction operator in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 185 Views

We are required to write a JavaScript function that takes in two numbers and returns their difference but without using the (-) sign. This problem can be solved using bitwise operations. The key insight is that subtraction can be performed using XOR (^) and AND (&) operations combined with bit shifting. How It Works The algorithm uses these bitwise operations: XOR (^): Performs subtraction without handling borrows AND (&): Identifies positions where borrowing is needed Left shift ( { console.log(`Step ${step}: a=${a}, b=${b}`); if(b === 0){ console.log(`Result: ${a}`); return a; } const xor = a ^ b; const borrow = (~a & b)

Read More

Dynamic programming to check dynamic behavior of an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 599 Views

We are required to write a JavaScript function that takes in an array of strings, ordered by ascending length. The function should return true if, for each pair of consecutive strings, the second string can be formed from the first by adding a single letter either at the beginning or end. For example: If the array is given by − const arr = ["c", "ca", "can", "acan", "acane", "dacane"]; Then our function should return true because: "ca" = "c" + "a" (adding "a" at the end) ...

Read More
Showing 281–290 of 5,881 articles
« Prev 1 27 28 29 30 31 589 Next »
Advertisements