Found 6710 Articles for Javascript

Map anagrams to one another in JavaScript

AmitDiwan
Updated on 26-Feb-2021 09:57:21

202 Views

Anagram arrays:One array is an anagram of another if we can randomise the elements of that array to achieve the other array.For example −[1, 2, 3] and [2, 1, 3] are anagrams of each other.Suppose, we have two arrays, arr1 and arr2 which are anagrams of each other.We are required to write a JavaScript function that takes in these two arrays and returns a new mapping array of the same length as arr1 and arr2. The mapping array should contain the index of elements of the arr1 array as they are present in the arr2 array.For example −If the two ... Read More

Calculating time taken to type words in JavaScript

AmitDiwan
Updated on 26-Feb-2021 09:59:42

288 Views

Suppose we have a keyword, which instead of the traditional qwerty type key mapping, maps keys simply according to the english alphabetic order i.e., abcde...Before we dive into the problem, we have to make the following two assumptions −Currently our fingertip is placed at index 0, i.e., the key 'aThe time taken to move from one key to some other is the absolute difference of their index, for example the time taken to move from 'a' to 'k' will be |0 - 10| = 10We are required to write a JavaScript function that takes in a string of english lowercase ... Read More

Squaring every digit of a number using split() in JavaScript

AmitDiwan
Updated on 26-Feb-2021 10:01:17

216 Views

We are required to write a JavaScript function that takes in a number as the first and the only argument. The function should then square every digit of the number, append them and yield the new number.For example −If the input number is −const num = 12349;Then the output should be −const output = 1491681;because '1' + '4' + '9' + '16' + '81' = 1491681ExampleThe code for this will be − Live Democonst num = 12349; const squareEvery = (num = 1) => {    let res = ''    const numStr = String(num);    const numArr = numStr.split('');   ... Read More

Checking if an array is sorted lexicographically in reference to some scrambled alphabet sequence in JavaScript

AmitDiwan
Updated on 26-Feb-2021 10:02:43

316 Views

We are required to write a JavaScript function that takes in an array of string words as the first argument. The second argument to the function will be a string containing all 26 English lowercase alphabets but in some random scrambled order.The task of our function is to check whether the words in the array are placed lexicographically correctly according to the order specified by the second argument. If it is so, we should return true, false otherwise.For example −If the input array of words and the order is −const arr = ['this', 'is', 'something', 'mad']; const order = 'hdetljnopqabcuvwxfgirsykmz';Then ... Read More

Adding paragraph tag to substrings within a string in JavaScript

AmitDiwan
Updated on 26-Feb-2021 08:08:14

592 Views

We are required to write a JavaScript function that takes in a string str as the first argument and an array of strings, arr as the second argument. We need to add a closed pair of paragraph tag and to wrap the substrings in str that exist in arr. If two such substrings overlap, we need to wrap them together by only one pair of closed paragraph tag.Also, if two substrings wrapped by paragraph tags are consecutive, we need to combine them.For example −If the input string and the array are −const str = 'kkkllmm'; const arr = ... Read More

2 Key keyboard problem in JavaScript

AmitDiwan
Updated on 24-Feb-2021 14:30:56

215 Views

Suppose the following situation −Initially on a notepad only one character 'A' is present. We can perform two operations on this notepad for each step −Copy All − We can copy all the characters present on the notepad (partial copy is not allowed).Paste − We can paste the characters which were copied last time.We are required to write a JavaScript function that takes in a number, let's call it num as the only argument. Our function is required to compute and return the minimum number of steps (copy all or paste) required to print 'A' num times.For example −If the input ... Read More

Finding three elements with required sum in an array in JavaScript

AmitDiwan
Updated on 24-Feb-2021 06:08:44

317 Views

We are required to write a JavaScript function that takes in an array of numbers as the first argument and a single number as the second argument. The function should then pick three such numbers from the array, (if they exist) whose sum is equal to the number specified by the second argument.The function should finally return an array of arrays of all such triplets if they exist, an empty array otherwise.For example −If the input array and the number is −const arr = [2, 5, 7, 8, 9, 11, 1, 6]; const sum = 22;Then the output should be ... Read More

Finding minimum deletions in string in JavaScript

AmitDiwan
Updated on 24-Feb-2021 16:06:56

178 Views

Suppose we have a binary string like this −const str = '001001';We are required to write a JavaScript function that takes in one such string as the first and the only argument.The function should then compute and return the number of minimum deletions required in the input so that no two adjacent numbers are the same.For example, for the above string, the output should be −const output = 2;because if we delete '0' at index 0 and 3, the new string will be '0101' which is the longest desired string.ExampleThe code for this will be − Live Democonst str = '001001'; ... Read More

Determining beautiful number string in JavaScript

AmitDiwan
Updated on 24-Feb-2021 06:03:08

356 Views

A numeric string, str, is called a beautiful string if it can be split into a sequence arr of two or more positive integers, satisfying the following conditions −arr[i] - arr[i - 1] = 1, for any i in the index of sequence, i.e., each element in the sequence is more than the previous element.No element of the sequence should contain a leading zero. For example, we can split '50607' into the sequence [5, 06, 07], but it is not beautiful because 06 and 07 have leading zeros.The contents of the sequence cannot be rearranged.For example −If the input string ... Read More

Calculating the weight of a string in JavaScript

AmitDiwan
Updated on 24-Feb-2021 16:09:53

836 Views

Weight of a character (alphabet):The weight of an English alphabet is nothing just its 1-based index.For example, the weight of 'c' is 3, 'k' is 11 and so on.We are required to write a JavaScript function that takes in a lowercase string and calculates and returns the weight of that string.ExampleThe code for this will be − Live Democonst str = 'this is a string'; const calculateWeight = (str = '') => {    str = str.toLowerCase();    const legend = 'abcdefghijklmnopqrstuvwxyz';    let weight = 0;    const { length: l } = str;    for(let i = 0; i ... Read More

Advertisements