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
Articles by AmitDiwan
Page 391 of 840
Check if a value exists in an array and get the next value JavaScript
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 MoreFinding Mode in a Binary Search Tree in JavaScript
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 MoreComparing adjacent element and swap - JavaScript?
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 MoreWrap object properties of type string with arrays - JavaScript
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 MoreLongest distance between 1s in binary JavaScript
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 MoreHighest occurrence in an array or first selected in JavaScript
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 MoreTraversing Diagonally in a matrix in JavaScript
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 MoreChange value of input on form submit in JavaScript?
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 MoreRemove Seconds/ Milliseconds from Date and convert to ISO String?
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 MoreReplacing upperCase and LowerCase in a string - JavaScript
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