Found 6683 Articles for Javascript

Decimal to binary conversion using recursion in JavaScript

AmitDiwan
Updated on 10-Dec-2020 08:18:31

386 Views

We are required to write a JavaScript function that takes in a number as the first and the only argument. The function should use recursion to construct a string representing the binary notation of that number.For example −f(4) = '100' f(1000) = '1111101000' f(8) = '1000'ExampleFollowing is the code −const decimalToBinary = (num) => {    if(num >= 1) {       // If num is not divisible by 2 then recursively return proceeding       // binary of the num minus 1, 1 is added for the leftover 1 num       if (num % 2) ... Read More

Reversing the order of words of a string in JavaScript

AmitDiwan
Updated on 10-Dec-2020 08:17:20

359 Views

We are required to write a JavaScript function that takes in a string as the only argument.The function should reverse the order of the words in the string and return the new string.The only condition is that we cannot use the inbuilt array method reverse().For example −If the input string is −const str = 'this is a string';Then the output string should be −const str = 'this is a string';ExampleFollowing is the code −const str = 'this is a string'; const reverseWordOrder = (str = '') => {    const strArr = str.split(' ');    let temp = '';   ... Read More

Unique intersection of arrays in JavaScript

AmitDiwan
Updated on 10-Dec-2020 08:15:35

229 Views

We are required to write a JavaScript function that takes in two arrays of numbers, let’s say arr1 and arr2. The function should find the intersection between the elements of the array. i.e., the elements that appear in both the arrays.The only condition is that if we encountered one element before as intersected, we should not consider it again even if appears again in both the arrays.For example −If the input arrays are −const arr1 = [1, 5, 7, 3, 1]; const arr2 = [1, 7, 3, 1, 6];Then the output array should be −const output = [1, 3, 7];However, ... Read More

Constructing product array in JavaScript

AmitDiwan
Updated on 10-Dec-2020 08:14:15

534 Views

We are required to write a JavaScript function that takes in an array of Numbers. The function should construct a new array based on the original array. Each corresponding element of the new array should be the product of all the elements of the original array including that element.For example −If the input array is −const arr = [1, 2, 3, 4, 5];Then the output array should be −const output = [120, 60, 40, 30, 24];We have to achieve this in linear time and constant space (obviously excluding the space used up in constructing the new array).ExampleFollowing is the code ... Read More

Largest difference between element with a twist in JavaScript

Nikitasha Shrivastava
Updated on 14-Aug-2023 18:38:03

89 Views

In this problem statement we have to find the largest difference between elements with a twist with the help of Javascript functionalities. So we will use basic Javascript to get the desired result. Understanding the problem The problem at hand is to find the largest difference between two items in an array. So we will add a twist to the problem. So we will not simply find the difference between two items but we will find the largest difference between an item and any smaller item that has appeared before in the array. And we will not rearrange ... Read More

Find the greatest product of three numbers in JavaScript

Nikitasha Shrivastava
Updated on 14-Aug-2023 17:18:44

393 Views

In the given problem statement our task is to find the greatest product of three numbers with the help of Javascript functionalities. So we will use sorting technique first to get the last three highest items of the array and then calculate the product of those three elements to get the desired result. Understanding the Problem The problem at hand is to find the greatest product of three items or numbers present in the given array in Javascript. So we will have an array of integers and we will identify three numbers whose product is the largest among ... Read More

Check whether we can form string2 by deleting some characters from string1 without reordering the characters of any string - JavaScript

Nikitasha Shrivastava
Updated on 11-Aug-2023 16:51:30

176 Views

In this given statement our task is to determine whether we can form string2 by removing some characters from the string1 without changing the order of the characters of any string with the help of Javascript functionalities. Understanding the problem The problem at hand is to check that the second string can be formed using the first string without changing the order of both the strings. For example suppose we have two strings as: s1 = "apple" s2 = "ale" Output - true So the output for the above strings should be true because the ... Read More

Pick out numbers from a string in JavaScript

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

130 Views

In the given problem we are required to find out the numbers from a given string with the help of Javascript functionalities. So we will use regular expressions and some predefined functions of Javascript. Understanding the Problem The problem at hand is to find the numbers from the given string in Javascript. This task can be useful sometimes. To accomplish this task we will use regular expressions and also some string manipulation functions of Javascript. So in simple terms we will be given a string in this string there will be some integer or other type of number ... Read More

Finding all duplicate numbers in an array with multiple duplicates in JavaScript

Nikitasha Shrivastava
Updated on 14-Aug-2023 17:27:51

629 Views

In the given problem statement we have to find all duplicate numbers in an array with multiple duplicates with the help of Javascript functionalities. So we will use basic Javascript tot solve this problem. Understanding the Problem The problem at hand is to find the duplicate numbers in an array with the help of JavaScript. So we will basically extract all the numbers which are appearing more than once in the given array. And in response we will get the array of repeated numbers. For example, suppose we have an array as [1, 1, 4, 8, 2, 2, ... Read More

Cumulative sum at each index in JavaScript

Nikitasha Shrivastava
Updated on 14-Aug-2023 15:24:42

696 Views

In the given problem statement we have to calculate the cumulative sum at each index with the help of Javascript functionalities. So we will use basic Javascript syntax and functions to solve this problem. What is Cumulative sum ? The cumulative sum is also known as running sum or prefix sum. So this sum is the calculation of the sum of a series of numbers up to a given index or position. In this process the numbers iteratively add each number in the series to the sum of the previous items. So the resultant new series in ... Read More

Advertisements