Found 6710 Articles for Javascript

Determining a pangram string in JavaScript

AmitDiwan
Updated on 24-Feb-2021 16:11:26

3K+ Views

Pangram strings:A pangram is a string that contains every letter of the English alphabet.We are required to write a JavaScript function that takes in a string as the first and the only argument and determines whether that string is a pangram or not. For the purpose of this problem, we will take only lowercase alphabets into consideration.ExampleThe code for this will be − Live Democonst str = 'We promptly judged antique ivory buckles for the next prize'; const isPangram = (str = '') => {    str = str.toLowerCase();    const { length } = str;    const alphabets = 'abcdefghijklmnopqrstuvwxyz'; ... Read More

Substring combination in JavaScript

AmitDiwan
Updated on 24-Feb-2021 16:12:50

235 Views

We are required to write a JavaScript function that takes in two strings as the first and the second argument. Let's call these strings str1 and str2. The function should check whether there exists a substring combination in str2, that when combined yields str2.By substring combination, we mean that we can skip characters but we have to maintain the order of the characters selected from str1.For example −If the input strings are −const str1 = 'desxooajmepwele'; const str2 = 'example';Then the output should be −const output = true;because the string 'example' can be formed by picking some and maintianing the ... Read More

Validating a password using JavaScript

Aayush Mohan Sinha
Updated on 04-Aug-2023 10:11:34

3K+ Views

The importance of robust password validation cannot be overstated in today's digital landscape, where online security is of paramount concern. Ensuring that user passwords meet certain criteria, such as complexity and length requirements, is vital in safeguarding sensitive information from unauthorized access. JavaScript, a versatile scripting language, provides developers with the necessary tools to implement effective password validation mechanisms. In this article, we delve into the intricate process of validating a password using JavaScript, elucidating the underlying algorithms and rarely utilized functions that empower developers to enhance the security of their web applications. By mastering this skill, developers can fortify ... Read More

Longest string with two distinct characters in JavaScript

AmitDiwan
Updated on 24-Feb-2021 16:16:39

385 Views

We are required to write a JavaScript function that takes in a string as the first argument and a number (smaller than the length of string) as the second argument. The function should delete characters from the original string and prepare a new string such that it's the longest string containing at most two distinct characters.Then at last the function should return the length of that desired string.For example: If the input string is −const str = 'kjeljsdl';Then the output should be −const output = 4;because the longest substring with at most 2 distinct characters is 'jljl'ExampleThe code for this ... Read More

Number of digits that divide the complete number in JavaScript

AmitDiwan
Updated on 24-Feb-2021 16:19:10

155 Views

We are required to write a JavaScript function that takes in a number as the first and the only argument. The function should count and return the number of digits present in the number that completely divide the number.For example −If the input number is −const num = 148;Then the output should be −const output = 2;because 148 is exactly divisible by 1 and 4 but not 8.ExampleThe code for this will be − Live Democonst num = 148; const countDividingDigits = (num = 1) => {    let count = 0;    const numStr = String(num);    for(let i = ... Read More

Longest subarray with absolute difference equal to some number in JavaScript

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

167 Views

We are required to write a JavaScript function that takes in an array of Numbers as the first argument, let's say arr, and a number, let's say num, as the second argument. The function should find and return the length of the longest subarray (contiguous or non-contiguous) whose each pair has an absolute difference less than or equal to num.For example, if the input array and the number are −const arr = [7, 9, 8, 6, 6, 3]; const num = 1;Then the output should be −const output = 3, because the desired subarray is [7, 6, 6]ExampleThe code for ... Read More

Finding matching pair from an array in JavaScript

AmitDiwan
Updated on 24-Feb-2021 16:22:32

2K+ Views

We are required to write a JavaScript function that takes in an array of Integers that might contain some repeating values. Our function should find out the number of pairs of identical integers we can extract out of the array.For example −If the input array is −const arr = [1, 5, 2, 1, 6, 2, 2, 9];Then the output should be −const output = 2;because the desired pairs are 1, 1 and 2, 2ExampleThe code for this will be − Live Democonst arr = [1, 5, 2, 1, 6, 2, 2, 9]; const countPairs = (arr = []) => {   ... Read More

Finding a pair that is divisible by some number in JavaScript

AmitDiwan
Updated on 24-Feb-2021 16:29:07

314 Views

We are required to write a JavaScript function that takes in an array of Numbers as the first argument, let's call it arr and a single number as the second argument, let's call it num.The function should find all such pairs from the array where −arr[i] + arr[j] = num, and i < jFor example −If the input array and the number is −const arr = [1, 2, 3, 4, 5, 6]; const num = 4;Then the output should be −const output = [    [1, 3], [2, 6], [3, 5] ];ExampleThe code for this will be − Live Democonst arr ... Read More

Finding desired sum of elements in an array in JavaScript

AmitDiwan
Updated on 24-Feb-2021 16:35:34

268 Views

Suppose we have an array of Numbers like this −const arr = [1, 2, 1, 3, 2];We are required to write a JavaScript function that takes in one such array as the first argument. The second argument will be a number that represents a desired sum, let us call it sum, and the third and the last argument will also be a number that represents the count of numbers that should add up to the desired sum from the array (without repetition of elements), let's call this number num.The function should finally return the number of all such groups that ... Read More

Sum excluding one element in JavaScript

AmitDiwan
Updated on 24-Feb-2021 16:38:37

738 Views

Suppose we have an array of Integers like this −const arr = [12, 1, 4, 8, 5];We are required to write a JavaScript function that takes in one such array as the only argument.The function should then return an array of exactly two integers −First integer should be the smallest possible sum of all the array elements excluding any one element.Second integer should be the greatest possible sum of all the array elements excluding any one element.The only condition for us is that we have to do this using one and only one for loop.For example −For the above array, ... Read More

Advertisements