Found 6710 Articles for Javascript

Substring in infinitely extended string in JavaScript

AmitDiwan
Updated on 23-Jan-2021 06:32:06

159 Views

We are required to write a JavaScript function that takes in a string of characters as the first argument and a start index and end index as second and third argument respectively. The function should find, had that string, provided as the first argument, been extended forever by appending the same string at end each time, what would have been the substring encapsulated by the start index and the end index.For example −If the input string and the indices are −const str = 'helloo'; const start = 11; const end = 15;Then the output should be −const output = 'hel';ExampleFollowing ... Read More

Checking if one string can be achieved from another with single tweak in JavaScript

AmitDiwan
Updated on 22-Jan-2021 07:11:59

95 Views

We are required to write a JavaScript function that takes in two strings of characters lets call them str1 and str2.The function should check whether we can form str2 from str1 by deleting exactly one character from str1. If we can do so the function should return true, false otherwise.For example −If the input strings are −const str1 = 'chemistty'; const str2 = 'chemisty';Then the output should be −const output = true;ExampleFollowing is the code −const str1 = 'chemistty'; const str2 = 'chemisty'; const stringSimilarity = (str1 = '', str2 = '') => {    if(str1.length - str2.length !== 1){ ... Read More

Sorting Integers by The Number of 1 Bits in Binary in JavaScript

AmitDiwan
Updated on 22-Jan-2021 07:10:16

514 Views

We are required to write a JavaScript function that takes in an array of integers as the only argument.The function should sort the integers present in the array in increasing order based on the 1s present in their binary representation. If two or more numbers have the same number of 1s in their binary, they should be sorted in increasing order according to their magnitude.For example −If the input array is −const arr = [34, 37, 23, 89, 12, 31, 23, 89];Then the output array will be −const output = [34, 12, 37, 23, 89, 23, 89, 31];ExampleFollowing is the ... Read More

Swapping even and odd index pairs internally in JavaScript

AmitDiwan
Updated on 22-Jan-2021 07:08:39

520 Views

We are required to write a JavaScript function that takes in an array of literals as the first and the only argument.Our function should swap each consecutive even index with each other, and swap each consecutive odd indexes with each other.The function should do these swappings in place.For example −If the input array is −const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8];Then the array should become −const output = [2, 3, 0, 1, 6, 7, 4, 5, 8];because 0 and 2 gets swapped, 1 and 3 gets swapped, 4 and 6 gets swapped, 5 and 7 ... Read More

Find Equivalent Value and Frequency in Array in JavaScript

AmitDiwan
Updated on 22-Jan-2021 07:06:19

98 Views

We are required to write a JavaScript function that takes in an array of integers as the only argument.The function should check whether there exists an integer in the array such that its frequency is same as its value.If there exists at least one such integer, we should return that integer otherwise we should return -1.For example −If the input array is −const arr = [3, 4, 3, 8, 4, 9, 7, 4, 2, 4];Then the output should be −const output = 4;ExampleFollowing is the code −const arr = [3, 4, 3, 8, 4, 9, 7, 4, 2, 4]; const ... Read More

Special type of sorting algorithm in JavaScript

AmitDiwan
Updated on 22-Jan-2021 07:05:11

101 Views

We are required to write a JavaScript function that takes in an array of integers as the only argument.The function should sort the array based on the following conditions −All even numbers are sorted in increasing orderAll odd numbers are sorted in decreasing orderThe relative positions of the even and odd numbers remain the sameFor example −If the input array is −const arr = [12, 17, 15, 24, 1, 6];Then the output should be −const output = [6, 17, 15, 12, 1, 24];ExampleFollowing is the code −const arr = [12, 17, 15, 24, 1, 6]; const specialSort = (nums = ... Read More

Calculating a number from its factorial in JavaScript

AmitDiwan
Updated on 22-Jan-2021 07:03:19

172 Views

We are required to write a JavaScript function that takes in a number as the only argument.The function should check whether there exists any number whose factorial is the number taken as input.If there exists any such number, we should return that number otherwise we should return -1.For example −If the input is −const num = 720;Then the output should be −const output = 6;ExampleFollowing is the code −const num = 720; const checkForFactorial = num => {    let prod = 1, count = 1;    while(prod

Unique number of occurrences of elements in an array in JavaScript

AmitDiwan
Updated on 22-Jan-2021 06:59:06

511 Views

We are required to write a JavaScript function that takes in an array of integers as the first and the only argument.The function should whether all the integers that are present in the array appear for unique number of times or not.If they do, the function should return true, false otherwise.For example −If the input array is −const arr = [7, 5, 5, 8, 2, 4, 7];Then the output should be −const output = false;because both the integers 7 and 5 appears for 2 times each.We will first use a hash map to map integers to their frequencies(occurrences) and then ... Read More

Finding a number and its nth multiple in an array in JavaScript

AmitDiwan
Updated on 22-Jan-2021 06:56:59

202 Views

We are required to write a JavaScript function that takes in an array of integers as the first argument and a number, say n, as the second argument.The function should check whether there exists two such numbers in the array that one is the nth multiple of the other.If there exists any such pair in the array, the function should return true, false otherwise.For example −If the array and the number are −const arr = [4, 2, 7, 8, 3, 9, 5]; const n = 4;Then the output should be −const output = true;because there exist the numbers 2 and ... Read More

Largest product of n contiguous digits of a number in JavaScript

AmitDiwan
Updated on 22-Jan-2021 06:54:53

169 Views

We are required to write a JavaScript function that takes in two numbers as first and the second argument, let us call them m and n.The first number will generally be a number with multiple digits and the second number will always be smaller than the number of digits in the first number.The function should find the group of n consecutive digits from m whose product is the greatest.For example −If the input numbers are −const m = 65467586; const n = 3;Then the output should be −const output = 280;because 7 * 5 * 8 = 280 and it’s ... Read More

Advertisements