AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 391 of 840

Check if a value exists in an array and get the next value JavaScript

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

We are required to write a JavaScript function that takes in an array of literals as the first argument and a search string as the second argument. The function should search the array for that search string. If that string exists in the array, we should return its next element from the array, otherwise we should return false. Example const arr = ["", "comp", "myval", "view", "1"]; const getNext = (value, arr) => { const a = [undefined].concat(arr); const p = a.indexOf(value) + 1; ...

Read More

Finding Mode in a Binary Search Tree in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 395 Views

Mode of a set of data is the number that occurs most frequently. For instance, 3 is the mode of dataset [2, 3, 1, 3, 4, 2, 3, 1] as it appears three times, more than any other number. Binary Search Tree A Binary Search Tree (BST) is a tree data structure where: The left subtree of a node contains only nodes with keys less than or equal to the node's key. The right subtree of a node contains only nodes with keys greater than or equal to the node's ...

Read More

Comparing adjacent element and swap - JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 477 Views

Bubble sort is a simple sorting algorithm that compares adjacent elements and swaps them if they are in the wrong order. This process repeats until the array is sorted. How Bubble Sort Works The algorithm repeatedly steps through the array, compares adjacent elements, and swaps them if they are in the wrong order. Each pass "bubbles" the largest element to its correct position. Bubble Sort Process Initial: 10 30 5 ...

Read More

Wrap object properties of type string with arrays - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 593 Views

When working with objects, you may need to ensure all properties are arrays. This is useful for normalizing data structures where some properties might be strings while others are already arrays. The Problem Consider an object where some properties are strings and others are arrays. To process them uniformly, you need all properties to be arrays: var details = { name: ["John", "David"], age1: "21", age2: "23" }; console.log("Original object:"); console.log(details); Original object: { name: [ ...

Read More

Longest distance between 1s in binary JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 466 Views

We are required to write a JavaScript function that takes a positive integer n and finds the longest distance between any two adjacent 1's in its binary representation. If there are no two adjacent 1's, the function returns 0. Two 1's are adjacent if there are only 0's separating them (possibly no 0's). The distance between two 1's is the absolute difference between their bit positions. Understanding the Problem For example, in the binary representation "1001", the two 1's have a distance of 3 (positions 0 and 3). Let's examine the number 22: ...

Read More

Highest occurrence in an array or first selected in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 196 Views

We are required to write a JavaScript function that takes in an array of literal values. Our function should then return the highest occurrence of an array value, and if there's an equal occurrence, we should return the first selected value of the equal occurrences. Problem Statement Given an array of values, find the element that appears most frequently. If multiple elements have the same highest frequency, return the one that appears first in the array. const arr1 = ['25', '50', 'a', 'a', 'b', 'c']; // 'a' appears 2 times (most frequent), so return 'a' ...

Read More

Traversing Diagonally in a matrix in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 946 Views

In JavaScript, traversing a matrix diagonally means moving through the elements in a zigzag pattern from top-left to bottom-right. This technique is useful for various algorithms including matrix processing and image manipulation. Problem Statement We need to write a JavaScript function that takes a square matrix (array of arrays with equal rows and columns) and traverses it diagonally, creating a new array with elements in the order they were encountered. For example, given this input matrix: const arr = [ [1, 2, 3], [4, 5, 6], ...

Read More

Change value of input on form submit in JavaScript?

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

In JavaScript, you can change the value of an input field when a form is submitted using the document object. This is useful for modifying form data before submission or performing dynamic updates. Syntax document.formName.inputName.value = newValue; Basic Example Here's a form with a hidden input field that gets modified on submit: Change Input Value on Submit ...

Read More

Remove Seconds/ Milliseconds from Date and convert to ISO String?

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

In JavaScript, you can remove seconds and milliseconds from a Date object and convert it to an ISO string format. This is useful when you need precise time formatting without the seconds component. Getting the Current Date First, let's create a Date object with the current date and time: var currentDate = new Date(); console.log("The current date is: " + currentDate); The current date is: Sat Aug 01 2020 18:20:09 GMT+0530 (India Standard Time) Removing Seconds and Milliseconds Use the setSeconds() method to set both seconds and milliseconds to 0: ...

Read More

Replacing upperCase and LowerCase in a string - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 676 Views

We are required to write a JavaScript function that takes in a string and constructs a new string with all the uppercase characters converted to lowercase and all the lowercase characters converted to uppercase. Let's write the code for this function — Example Following is the code — const str = 'The Case OF tHis StrinG Will Be FLiPped'; const isUpperCase = char => char.charCodeAt(0) >= 65 && char.charCodeAt(0) char.charCodeAt(0) >= 97 && char.charCodeAt(0) { let newStr = ''; const margin = 32; ...

Read More
Showing 3901–3910 of 8,392 articles
« Prev 1 389 390 391 392 393 840 Next »
Advertisements