Found 6685 Articles for Javascript

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

252 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

JavaScript Program to Find a triplet such that sum of two equals to third element

AmitDiwan
Updated on 13-Mar-2023 16:47:59

185 Views

We will be writing a JavaScript program that finds a triplet where the sum of two elements equals the third element. This program will be implemented using arrays and loop structures. We will be iterating through the array and checking for each element if the sum of two elements equal to the current element. If we find such a triplet, we will immediately return it. This program will be helpful in various mathematical computations where we need to find such triplets that follow a specific rule. Approach Here is one approach to solving the problem of finding a triplet such ... Read More

JavaScript Program to Efficiently compute sums of diagonals of a matrix

AmitDiwan
Updated on 13-Mar-2023 16:44:30

231 Views

We are going to write a program in JavaScript that efficiently computes the sum of the diagonals of a matrix. To do this, we will utilize a loop structure that iterates through the matrix and adds the elements located at the positions that correspond to the diagonals. By taking advantage of the mathematical properties of a matrix, we can minimize the amount of computation required to find the sum of the diagonals. With this approach, we will be able to handle matrices of various sizes in a computationally efficient manner. Approach To compute the sum of diagonals of a ... Read More

JavaScript Program To Delete Nodes Which Have A Greater Value On Right Side

AmitDiwan
Updated on 13-Mar-2023 16:42:33

205 Views

We will be implementing a function to delete nodes in a linked list which have a greater value on their right side. The approach is to traverse the linked list from right to left and keep track of the maximum value encountered so far. For each node, we will compare its value with the maximum value and delete the node if its value is less than the maximum value. This way, all the nodes with values greater than the maximum value on their right side will be deleted. Approach The approach to delete nodes which have a greater value on ... Read More

JavaScript Program To Delete Alternate Nodes Of A Linked List

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

155 Views

We will be writing a JavaScript program to delete alternate nodes of a linked list. We will be utilizing a while loop to traverse the linked list while keeping a track of the current and previous node. In each iteration of the loop, we will be skipping the current node and linking the previous node directly to the next node, effectively deleting the current node from the list. This process will be repeated until all the alternate nodes have been deleted from the linked list. Approach Traverse the linked list from head to end. For every node, store ... Read More

JavaScript Program to Count triplets with sum smaller than a given value

AmitDiwan
Updated on 13-Mar-2023 16:30:56

249 Views

We will be writing a JavaScript program to count the number of triplets with a sum smaller than a given value. This problem can be solved by sorting the array and using two pointers to check for the possible combinations. Firstly, we will sort the array in ascending order and then, for each element in the array, we will use two pointers to check for the triplets with a sum less than the given value. The number of such triplets will be the count we will be keeping track of. Additionally, we will be updating the count and the pointers ... Read More

JavaScript Program to Count rotations which are divisible by 10

AmitDiwan
Updated on 13-Mar-2023 16:28:44

156 Views

We are going to write a program in JavaScript that counts the number of rotations of a given number which are divisible by 10. We will loop through the rotations of the number and check if each one is divisible by 10. If a rotation is divisible, we will increment our count. In the end, we will return the count as the result of our program. This program will be useful in various applications as it provides a simple solution to check the divisibility of a number by 10. Approach The approach to solve this problem is as follows − ... Read More

JavaScript Program to Count rotations required to sort given array in non-increasing order

AmitDiwan
Updated on 13-Mar-2023 16:24:04

95 Views

We will be writing a program to count the number of rotations required to sort an array in non-increasing order. The program will use a loop to traverse the array and keep track of the maximum element found so far. When a smaller element is found, we will increment the rotation count and update the maximum element. In the end, the rotation count will be returned as the result of the program. This program will help us efficiently sort the array and determine the number of rotations required to achieve a non-increasing order. Approach The approach to counting rotations required ... Read More

Get the relative timestamp difference between dates in JavaScript

Rushi Javiya
Updated on 10-Mar-2023 16:53:36

4K+ Views

Have you ever seen notifications on any website showing the time stamp? It shows something like “12 minutes ago”, “2 days ago”, “10 hours ago”, etc. It is related to the timestamp difference between two dates or times. Also, some apps show that this device's last login was 22 hours ago. So, there are many uses to get the timestamp difference between two dates. In this tutorial, we will learn different approaches to get the relative timestamp difference between two dates. Use the getTime() method with the date and create a custom algorithm In JavaScript, we can ... Read More

Advertisements