Found 6683 Articles for Javascript

2 Key keyboard problem in JavaScript

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

156 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

238 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

133 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

251 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

660 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

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

151 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

2K+ 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

300 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

101 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

Advertisements