AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 382 of 840

Checking existence of all continents in array of objects in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 182 Views

Problem We are required to write a JavaScript function that takes in an array of objects that contains data about the continent of belonging for some people. Our function should return true if it finds six different continents in the array of objects, false otherwise. Example Following is the code − const people = [ { firstName: 'Dinesh', lastName: 'A.', country: 'Algeria', continent: 'Africa', age: 25, language: 'JavaScript' }, { firstName: 'Ishan', lastName: 'M.', country: 'Chile', continent: 'South America', age: 37, language: 'C' }, ...

Read More

How do I check that a number is float or integer - JavaScript?

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

In JavaScript, you can check if a number is a float (decimal) or integer using various methods. The most reliable approach uses the modulo operator to detect decimal values. Method 1: Using Modulo Operator The modulo operator % returns the remainder after division. For floats, value % 1 returns the decimal part: function checkNumberIfFloat(value) { return Number(value) === value && value % 1 !== 0; } var value1 = 10; var value2 = 10.15; if (checkNumberIfFloat(value1)) { console.log("The value is float=" + value1); } else { ...

Read More

Greater possible digit difference of a number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 235 Views

We are required to write a JavaScript function that takes in a number. Then the function should return the greatest difference that exists between any two digits of the number. In other words, the function should simply return the difference between the greatest and the smallest digit present in it. Example Problem If the number is 654646, Then the smallest digit here is 4 and the greatest is 6 Hence, our output should be 2 Using Recursive Approach The recursive solution extracts digits one by one and keeps track of minimum and maximum ...

Read More

Implementing binary search in JavaScript to return the index if the searched number exist

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 714 Views

We are required to write a JavaScript function that takes in a sorted array of numbers as the first argument and a search number as the second argument. If the search number exists in the array, we need to return its index in the array, otherwise we need to return -1. We have to do this making use of the binary search algorithm. The binary search algorithm is basically a divide and conquer algorithm which recursively divides the array into halves until it converges to a singleton element. The sorting of array is necessary for binary search ...

Read More

Using a recursive function to capitalize each word in an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 467 Views

We are required to write a JavaScript function that takes in an array of String literals. The function should do the following two things − Make use of recursive approach Make first word of each string element capital. Our function should do this without using extra space for storing another array. For example − If the input array is − const arr = ['apple', 'banana', 'orange', 'grapes']; Then the array should be transformed to − const output = ['Apple', 'Banana', 'Orange', 'Grapes']; ...

Read More

Replacing digits to form binary using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 600 Views

We need to write a JavaScript function that converts a string of digits into a binary-like representation by replacing digits below 5 with '0' and digits 5 and above with '1'. Problem Statement Given a string containing digits, transform it using these rules: Digits 0, 1, 2, 3, 4 → '0' Digits 5, 6, 7, 8, 9 → '1' Using For Loop const str = '262355677834342'; const convert = (str = '') => { let res = ''; for(let i = 0; i < ...

Read More

Merging and rectifying arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 192 Views

We need to create a JavaScript function that merges two arrays and removes duplicates, ensuring each element appears only once in the final result. Problem We are required to write a JavaScript function that takes in two arrays of numbers, arr1 and arr2, as the first and the second arguments. Our function should merge the elements of both these arrays into a new array and if upon merging or before merging there exists any duplicates, we should delete the excess duplicates so that only one copy of each element is present in the merged array. The ...

Read More

Add property to common items in array and array of objects - JavaScript?

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

To add properties to objects based on common items in arrays, use the map() method combined with Set for efficient lookups. This approach allows you to conditionally add properties to array objects based on whether their values exist in another array. Example Arrays Let's start with a simple array and an array of objects: const firstname = ['John', 'David', 'Bob']; const studentDetails = [ { firstname: 'Carol', marks: 78 }, ...

Read More

Merge and remove duplicates in JavaScript Array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 445 Views

Merging arrays and removing duplicates is a common task in JavaScript. There are several approaches to accomplish this, from traditional loops to modern ES6 methods. Problem Statement Given two arrays of numbers, we need to combine them into a single array where each element appears only once. const arr1 = [2, 4, 5, 3, 7, 8, 9]; const arr2 = [1, 4, 5, 2, 3, 7, 6]; console.log("Array 1:", arr1); console.log("Array 2:", arr2); Array 1: [ 2, 4, 5, 3, 7, 8, 9 ] Array 2: [ 1, 4, 5, 2, 3, 7, ...

Read More

Finding smallest element in a sorted array which is rotated in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 189 Views

We are required to write a JavaScript function that takes in an array of integers as the only argument. The array is first sorted and then rotated by any arbitrary number of elements. Our function should find the smallest element in the array and return that element. The only condition is that we have to do this in less than linear time complexity, using a modified binary search algorithm that achieves O(log n) time complexity. Problem Understanding In a rotated sorted array like [6, 8, 12, 25, 2, 4, 5], the original sorted array [2, 4, ...

Read More
Showing 3811–3820 of 8,392 articles
« Prev 1 380 381 382 383 384 840 Next »
Advertisements