Found 6710 Articles for Javascript

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

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

138 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 12-Dec-2024 16:40:26

497 Views

To find difference between sums of two diagonals, we will be discussing two different approaches. We will calculate the sum of the elements present in the left and right diagonal, then we will subtract both sum values to get the difference. In this article we are having a square matrix, our task is to write a JavaScript program to find difference between sums of two diagonals. Example Input: Matrix: [[1, 2, 3], [4, 5, 6], [9, 8, 9]] Sum of left diagonal elements = 1+5+9 = 15 Sum of right diagonal elements = 3+5+9 ... Read More

JavaScript Program to Find Closest Number in Array

AmitDiwan
Updated on 05-Dec-2024 15:09:54

3K+ Views

To find closest number in array using Javascript, we will be understanding and discussing three different approaches. We will also compare time and space complkexities of all the appproaches used. In this article we are having an array and a target value, our task is to find closest number to target value in array using Javascript. User must know about JavaScript arrays, searching alogorithms such as linear and binary search, sorting, loops, and conditional statement. Approaches to Find Closest Number in Array Here is a list of approaches to find closest number in array in JavaScript which we will ... 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

196 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

16K+ 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 28-Nov-2024 17:10:17

19K+ Views

To find area of a circle using javascript, we will be using simple formulae of calculating the area of circle. The formula used is: Area = π × r² where r is the radius and π is a constant value pi. In this article, we are having radius of the circle and our task is to find the area of the circle using JavaScript. Example let radius = 10 and π(pi) = 3.1415926535 Area = π × r² Area= 3.1415926535 * 10 * 10 Area= 314.15926535 Steps to Find Area of Circle ... Read More

JavaScript Program to Find Area and Perimeter of Rectangle

Disha Verma
Updated on 18-Mar-2025 12:59:21

9K+ Views

To find the area and perimeter of a rectangle in JavaScript, we will be using the basic formulas for the perimeter and area of a rectangle. The area of a rectangle is the multiplication of its length by its breadth, and the perimeter of a rectangle is the sum of the lengths of all its four sides. In this article, we are going to understand the formula for the area and perimeter of a rectangle, how to find them using JavaScript, and implement practical examples for this. Understanding the Formulas The formulas for the area and perimeter of a ... Read More

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

Alshifa Hasnain
Updated on 27-Dec-2024 19:17:21

522 Views

In this article, we will learn to find three numbers in an array that add up to a given sum value using JavaScript. We are going to write a JavaScript program to find a triplet that sums up 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. We'll present two different approaches and analyze their implementations. Sorting and Two-Pointer Technique The first approach uses sorting and the two-pointer technique to solve the problem efficiently. This method ... 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

316 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

Ravi Ranjan
Updated on 06-Jun-2025 19:11:29

413 Views

To efficiently compute sums of diagonals of a matrix, we will be discussing two different approaches. We will calculate the sum of the elements present in the left-to-right and right-to-left diagonal i.e primary and secondary diagonal respectively. In this article we are having a square matrix, our task is to write a JavaScript program to efficiently compute sums of diagonals of a matrix. Example Input: Matrix: [[1, 2, 3], [4, 5, 6], [9, 8, 9]] Sum of primary diagonal elements = 1+5+9 Sum of secondary diagonal elements = 3+5+9 Output: Sum of ... Read More

Advertisements