Found 6683 Articles for Javascript

Subarray sum with at least two elements in JavaScript

AmitDiwan
Updated on 03-Mar-2021 07:47:25

122 Views

ProblemWe are required to write a JavaScript function that takes in an array of Integers, arr, as the first argument and a single Integer, target as the second and first argument. Our function should check whether there exists a continuous subarray of size at least 2 that sums up to the multiple of k, that is, sums up to n*k where n can be any integer.We return true if it exists, false otherwise.For example, if the input to the function is −const arr = [23, 2, 6, 4, 7]; const target = 6;Then the output should be −const output = ... Read More

Forming the longest word in JavaScript

AmitDiwan
Updated on 03-Mar-2021 07:49:42

213 Views

ProblemWe are required to write a JavaScript function that takes in a random English alphabet string, str, as the first argument and an array of strings, arr, as the second argument.The task of our function is to try deleting some characters from the string str and check which longest word can be formed that exists in the array arr as well. Our function should return the longest possible string.If there exists no such string, we should return an empty string.For example, if the input to the function is −const str = 'sdgfdfghdjh'; const arr = ['sdf', 'fghj', 'gfdfg', 'absc', 'a', ... Read More

Contiguous subarray with 0 and 1 in JavaScript

AmitDiwan
Updated on 03-Mar-2021 07:58:04

246 Views

Problem:We are required to write a JavaScript function that takes in a binary array, arr, (an array that only consists of 0 or 1). Our function should return the length of the contiguous subarray from the array that consists of the same number of 1 and 0.For example, if the input to the function is −const arr = [1, 0, 0, 1, 0, 1, 0, 0];Then the output should be −const output = 6;Output ExplanationThe first 6 elements of the array are 1, 0, 0, 1, 0, 1 (three 1s and three 0s)ExampleThe code for this will be − Live Democonst ... Read More

Beautiful Arrangement of Numbers in JavaScript

AmitDiwan
Updated on 03-Mar-2021 08:03:14

300 Views

Beautiful Arrangement:Suppose we have num integers from 1 to num. We define a beautiful arrangement as an array that is constructed by these num numbers successfully if one of the following is true for the ith position (1 ≤ i ≤ N) in this array −The number at the ith position is divisible by i.i is divisible by the number at the ith position.ProblemWe are required to write a JavaScript function that takes in a number, num, and returns the count of beautiful arrangements we can construct for num.For example, if the input to the function is −const input = ... Read More

Finding the sum of two numbers without using '+', '-', '/', '*' in JavaScript

AmitDiwan
Updated on 03-Mar-2021 08:06:18

756 Views

We are required to write a JavaScript function add() that takes in two numbers m and n. The function should, without using the four basic arithmetic operations add the two numbers taken as input and return the sum.ExampleThe code for this will be − Live Democonst m = 67, n = 33; const add = (x, y) => {    while(y !== 0){       let carry = x & y;       x = x ^ y;       y = carry

Finding nth digit of natural numbers sequence in JavaScript

AmitDiwan
Updated on 03-Mar-2021 08:10:01

456 Views

Natural Number Sequence:1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12...This sequence extended infinitely is known as natural number sequence.We are required to write a JavaScript function that takes in a number, num, as the first and the only argument. The function should find and return the (num)th digit that will appear in this sequence when written, removing the commas and whitespaces.For example −If the input number is −const num = 13;Then the output should be −const output = 1;because '1234567891011' this string has its 13th number as 1ExampleThe code for this will be − Live Democonst num ... Read More

Kit-Kat array in JavaScript

AmitDiwan
Updated on 03-Mar-2021 08:34:54

73 Views

We are required to write a JavaScript function that takes in a natural number, num, as the first argument and two natural numbers m and n as the second and third argument. The task of our function is to return an array that contains all natural numbers from 1 to num (including num) in increasing order.But if any number is a multiple of m, we should replace it by 'kit' string, if any number is a multiple of n, we should replace it by 'kat', andif any number is a multiple of both m and n it should be replaced ... Read More

Finding the third maximum number within an array in JavaScript

AmitDiwan
Updated on 03-Mar-2021 08:36:44

138 Views

We are required to write a JavaScript function that takes in an array of numbers as the first and the only argument.The task of our function is to pick and return the third maximum number from the array. And if the array does not contain any third maximum number then we should simply return the maximum number from the array.For example −If the input array is −const arr = [34, 67, 31, 87, 12, 30, 22];Then the output should be −const output = 34;ExampleThe code for this will be − Live Democonst arr = [34, 67, 31, 87, 12, 30, 22]; ... Read More

Finding minimum absolute difference within a Binary Search Tree in JavaScript

AmitDiwan
Updated on 03-Mar-2021 08:40:44

158 Views

We are required to write a JavaScript function that takes in the root of a BST that holds some numerical data like this −1 \ 3 / 2The function should return the minimum absolute difference between any two nodes of the tree.For example −For the above tree, the output should be −const output = 1;because |1 - 2| = |3 - 2| = 1ExampleThe code for this will be − Live Democlass Node{    constructor(data) {       this.data = data;       this.left = null;       this.right = null;    }; }; class BinarySearchTree{    constructor(){ ... Read More

Preparing encoding and decoding algorithms for shortening URLs in JavaScript

AmitDiwan
Updated on 03-Mar-2021 08:42:40

675 Views

We often come through services like bit.ly and tinyurl which takes in any url and (usually one bigger in length), performs some encryption algorithm over it and returns a very short url. And similarity when we try to open that tiny url, it again runs some decryption algorithm over it and converts the short url to the original one opens the link for us.We are also required to perform the same task. We are actually required to write two functions −encrypt() --> it will take in the original url and return to us a short unique ur.decrypt() --> it will take in ... Read More

Advertisements