Found 10711 Articles for Web Development

JavaScript Program to Find k maximum elements of array in original order

AmitDiwan
Updated on 15-Mar-2023 14:43:17

80 Views

We will be using the JavaScript array sort method and slicing technique to find the k maximum elements of an array in their original order. Firstly, we sort the array in descending order, and then slice it from the beginning to the kth index to obtain the k maximum elements. By preserving the original order of the elements, the significance and context of the data remains intact, making it easier for us to analyze and interpret the results. Approach The approach to find k maximum elements of an array in original order can be described as follows − Create ... Read More

JavaScript Program to Find if there is a subarray with 0 sum

AmitDiwan
Updated on 15-Mar-2023 14:36:11

349 Views

We, as developers, are often asked to find if there is a subarray with 0 sum in an array. This can be done by using the concept of prefix sum. We will keep track of the sum of elements of the subarray seen so far and store it in a hash map. If the sum is seen before, it means that the subarray with this sum exists and has 0 sum. We will be continuously updating the hash map with the sum of elements seen so far. This way, we can determine if there is a subarray with 0 sum ... Read More

JavaScript Program to Find element at given index after a number of rotations

AmitDiwan
Updated on 15-Mar-2023 14:33:24

68 Views

We will be implementing a JavaScript program to find an element at a given index after a number of rotations. This program will require us to perform rotations on an array and then return the element present at the specified index. To accomplish this task, we will be using the modulo operator to calculate the new index after each rotation. The future continuous tense will be used throughout the explanation. In the program, we will be taking the input of the array, the number of rotations, and the index. We will then perform the rotations by using the modulo operator ... Read More

JavaScript Program to Find difference between sums of two diagonals

AmitDiwan
Updated on 15-Mar-2023 14:31:38

295 Views

We will be finding the difference between the sums of two diagonals of a square matrix. Firstly, we will calculate the sum of the elements present in the first diagonal by traversing the matrix starting from the top left corner to the bottom right corner. Secondly, we will calculate the sum of the elements present in the second diagonal by traversing the matrix starting from the top right corner to the bottom left corner. Finally, we will subtract the sum of the second diagonal from the sum of the first diagonal to get the difference between the two diagonals. Approach ... Read More

JavaScript Program to Find closest number in array

AmitDiwan
Updated on 15-Mar-2023 14:30:35

2K+ Views

We will write a JavaScript program to find the closest number in an array by comparing each element with the target number and keeping track of the closest one. The program will use a loop to go through each element in the array and use a conditional statement to compare the difference between the target number and the current element. If the difference is smaller than the current closest difference, we will update the closest number. The result of this program will be the closest number to the target in the given array. Approach This program finds the closest number ... Read More

JavaScript Program to Find array sum using Bitwise OR after splitting given array in two halves after K circular shifts

AmitDiwan
Updated on 15-Mar-2023 14:29:03

143 Views

We will write a JavaScript program to find the sum of an array by using bitwise OR after splitting the given array into two halves after K circular shifts. Our program will perform the task by taking an array and an integer K as inputs. First, we will split the array into two halves after performing K circular shifts. Then, we will perform bitwise OR on both the halves to get a new array. Finally, we will find the sum of the new array obtained from the bitwise OR operation. Approach First, perform K circular shifts on the given ... Read More

How to check empty/undefined/null strings in JavaScript?

Mohit Panchasara
Updated on 15-Mar-2023 12:18:59

15K+ Views

In JavaScript, “” represents the empty string, and we can use the null keyword to initialize the string with a null value. If we don’t assign any value to any variable, it is undefined by default. Sometimes, we need to check if the string is empty, undefined, or null while working with the strings. For example, we are taking the details from the users via an HTML form, and users can add an empty string to the input field; we need to validate the input field and show the error message to the users. In this tutorial, we will learn ... Read More

JavaScript program to find area of a circle

AmitDiwan
Updated on 14-Jun-2024 14:53:59

14K+ Views

We will be using the below formula to find the area of a circle − pi * r^2 In the program, we will prompt the user to input the radius of the circle. Then, we will use the formula to calculate the area and print it on the screen. Approach Get the radius of the circle from the user as an input. Calculate the area using the formula: area = π * radius^2 Store the value of π, it can be calculated using Math.PI in JavaScript. Multiply the value of π with the square of ... Read More

JavaScript program to find Area and Perimeter of Rectangle

AmitDiwan
Updated on 21-Jun-2024 12:19:35

5K+ Views

We are writing a JavaScript program to calculate the area and perimeter of a rectangle. The program will prompt the user to input the width and length of the rectangle, and then we will use these values to calculate the area and perimeter. We will be continuously using these formulas: area = width * length, and perimeter = 2 * (width + length) to find the desired measurements. Approach The approach to find the Area and Perimeter of a rectangle in JavaScript can be done as follows − Define the length and width of the rectangle using variables. Calculate ... Read More

JavaScript Program to Find a triplet that sum to a given value

AmitDiwan
Updated on 13-Mar-2023 16:51:34

254 Views

We are going to write a JavaScript program to find a triplet that sum up to a given value. This program will make use of nested loops to iterate over the input array and check for the existence of a triplet with a sum equal to the given value. Our program will continuously search for a triplet until it is found or all possible combinations have been exhausted. This program will make it possible for us to find a triplet that sum up to a given value in an efficient and straightforward manner. Approach The approach to find a triplet ... Read More

Advertisements