Found 6710 Articles for Javascript

Function for counting frequency of space separated elements in JavaScript

Nikitasha Shrivastava
Updated on 14-Aug-2023 17:55:26

272 Views

In this problem statement, our target is to create a function for counting frequency of space separated elements with the help of Javascript functionalities. Understanding the Problem The given problem is stating that we have given a space separated elements and our task is to count the frequency of space separated items. So we will provide some strings with some space separated items and assign a result array to see the counts of the frequency of such items. For example if we have a string with space separated items like “Car, Bike, Car, Bicycle, Bike”, in this ... Read More

Sorting according to weights of numbers in JavaScript

Nikitasha Shrivastava
Updated on 16-Aug-2023 16:55:33

1K+ Views

In this problem statement, our target is to sort the given array of numbers according to the weights of numbers with the help of Javascript. At the beginning we will write a function that calculates the weights of the numbers and then sort them with another function. Understanding the Problem We have to sort the provided numbers according to their weights. As a result we will have an array of numbers. A weight of the number is defined as the sum of its own digits. For example: suppose we have an array like [19, 11, 12, 15]. We ... Read More

JavaScript program to convert positive integers to roman numbers

Nikitasha Shrivastava
Updated on 30-Sep-2024 16:05:43

736 Views

We have to write a JavaScript program to generate the Roman number representation of given integer numbers. The code should accept a positive integer as input and will be able to transform the integer into its equivalent Roman number. Example Scenario: Input: num = 10; Output: roman_num = X How to Convert Integers to Roman Numbers To solve the given problem, define a function to convert an integer to roman. This function will take a positive integer as input and will return the corresponding Roman numeral as a string. It will use an array of objects to map the ... Read More

How to reverse a string using only one variable in JavaScript

Nikitasha Shrivastava
Updated on 14-Aug-2023 18:19:30

257 Views

In this problem statement, our target is to print the reverse string using only one variable and implement the solution with the help of Javascript. So we can solve this problem with the help of loops in Javascript. Understanding the problem The given problem is stating that we have given a string to which we have to reverse the string. In simple terms we can say that if we have the string “Hello World”, the reverse of this string will be “dlroW ,olleH”. Logic for the given problem In order to reverse the given ... Read More

Shortest distance between objects in JavaScript

Nikitasha Shrivastava
Updated on 16-Aug-2023 16:50:13

568 Views

In this problem we have to calculate and find the shortest distance between two given objects in Javascript. So we will write a function to do this operation and also we will see its time and space complexity. We must consider the positions and coordinates of the objects in some way. Understanding the Problem The problem says to find the shortest path between the two given objects. So we will use two coordinates. And define a function which will have four parameters. These parameters will represent the x and y coordinates of the two objects. Inside the function ... Read More

Group a sorted array based on the difference between current and previous elements in JavaScript

Nikitasha Shrivastava
Updated on 14-Aug-2023 18:10:05

467 Views

In the given problem statement we have to group a sorted array based on the difference between the current and previous elements with the help of Javascript functionalities. To group a sorted array based on the difference between the current and previous items we can iterate over the array and create a new array of groups. Understanding the Problem Statement The above problem statement states that we have to find out the group of elements based on the difference between the current and previous elements in the array. As we have given a sorted array so we need ... Read More

Highest and lowest value difference of array JavaScript

Nikitasha Shrivastava
Updated on 14-Aug-2023 18:14:24

561 Views

In this problem we have to create an algorithm to get the highest and lowest value difference of an array with the help of Javascript functionality. So we will solve the problem with for loop and initialization of highest and lowest values to Infinity. Understanding the logic of the problem statement The problem statement is asking to write the code for getting the highest and lowest value difference of the array. So for solving this task we will initialize two variables, these variables will store the highest and lowest values. And then iterate through every element ... Read More

JavaScript Sum odd indexed and even indexed elements separately and return their absolute difference

Nikitasha Shrivastava
Updated on 30-Sep-2024 16:03:09

1K+ Views

For a given list of elements, write a JavaScript program to find the sum of its odd and even indexed elements and then, calculate difference between them. To solve this problem, we will first separate odd indexed and even indexed elements. After separating them find their sum separately and store it in different variables. Now, we will calculate the difference between these sums to get desired result. Example Scenario: Input: list = [11, 21, 31, 41, 51, 61]; Output: difference = 30 Here, odd-indexed items are [21, 41, 61] and their sum is 123. The even-indexed items are ... Read More

Average with the Reduce Method in JavaScript

AmitDiwan
Updated on 11-Aug-2023 16:14:19

1K+ Views

In the given problem statement, our aim is to get the average of all the items with the help of the reduce method of Javascript. So for doing this task we will create a function and give an array as a parameter. Understanding the problem statement We have given a task to calculate the average value of the given items in the array. And we have to use the reduce method which is a predefined method of Javascript. For example if we have an array as [1, 2, 3, 4, 5], so the average value of the ... Read More

Remove duplicate items from an array with a custom function in JavaScript

Nikitasha Shrivastava
Updated on 16-Aug-2023 16:30:24

432 Views

In this problem statement our aim is to write an algorithm to remove duplicate items from an array with a function with the help of Javascript functionalities. Understanding the problem statement In the above problem statement we have to create a function by which we can accept an array as a parameter and give a new array to store the unique items from the given input array and without including the identical values. For example - suppose we have an array as [0, 0, 2, 2, 3, 3] so after removing the duplicate numbers we will have ... Read More

Advertisements