Found 10677 Articles for Web Development

Reversing strings with a twist in JavaScript

AmitDiwan
Updated on 03-Mar-2021 08:45:26

135 Views

We are required to write a JavaScript function that takes in a string str as the first argument and an integer num as the second argument.Our function should reverse the first num characters for every 2 * num characters counting from the start of the string. And if there are less than num characters left, we have to reverse all of them.If there are less than 2 * num but greater than or equal to num characters, then we have to reverse the first num characters and leave the other as original.For example −If the input string and the number ... Read More

Distance of nearest 0 in binary matrix in JavaScript

AmitDiwan
Updated on 03-Mar-2021 08:47:05

148 Views

A binary matrix is an array of arrays containing only 0 or 1. We are required to write a JavaScript function that takes in a binary matrix as the only argument.Our function should create a new matrix containing the same number of rows and columns, and for each element of the original matrix the resulting matrix should contain that element's nearest distance from 0 in the original matrix.We have to keep in mind that while calculating distance it can move either horizontally or vertically and not diagonally. And it's guaranteed that the matrix contains at least one 0.For example −If ... Read More

Finding longest line of 1s in a matrix in JavaScript

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

126 Views

Suppose, we have a binary matrix (an array of array that contains only 0 or 1) like this −const arr = [    [0, 1, 1, 0],    [0, 1, 1, 0],    [0, 0, 0, 1] ];We are required to write a JavaScript function that takes in one such matrix as the first and the only argument.The task of our function is to find the longest line of consecutive ones in the matrix and return the count of 1s in it. The line could be horizontal, vertical, diagonal or anti-diagonal.For example, for the above array, the output should be ... Read More

Is a number sum of two perfect squares in JavaScript

AmitDiwan
Updated on 03-Mar-2021 08:50:55

159 Views

Perfect Square Numbers:A natural number in mathematics is called a perfect square if it can be obtained by multiplying any other natural number into that very number.For instance, 9, 16, 81, 289 are all perfect squares.We are required to write a JavaScript function that takes in a natural number, say num, as the only argument. The function should determine whether there exists two such number m and n such that −(m * m) + (n * n) = numIf there exists such numbers, our function should return true, false otherwise.For example −If the input number is −const num = 389;Then ... Read More

Finding the maximum number using at most one swap in JavaScript

AmitDiwan
Updated on 03-Mar-2021 08:52:53

169 Views

We are required to write a JavaScript function that takes in a number as the first and the only argument.The task of our function is to perform at most one swap between any two digits of the number and yield the maximum possible number. If, however, the number is already the maximum possible number we should return the number itself.For example −If the input number is −const num = 1625;Then the output should be −const output = 6125;We swapped 1 and 6 and this is the only swap the yields the greatest number in single swapExampleThe code for this will ... Read More

Checking if a string can be made palindrome in JavaScript

Aayush Mohan Sinha
Updated on 04-Aug-2023 09:49:48

507 Views

Exploring the realm of string manipulation in JavaScript reveals a fascinating challenge: determining whether a given string can be transformed into a palindrome. Palindromes, words or phrases that read the same forwards and backwards, possess an inherent allure and pique the curiosity of developers seeking to unravel their mystical properties. In this article, we embark on an enlightening journey to unravel the intricacies of checking if a string can be made into a palindrome using the powerful language features and algorithms inherent to JavaScript. By delving into the depths of string manipulation and employing innovative techniques, we unravel the enigma ... Read More

Finding n most frequent words from a sentence in JavaScript

AmitDiwan
Updated on 27-Feb-2021 17:08:31

1K+ Views

For the purpose of this question, we define a sentence as a string that contains English alphabets and punctuations and a word is a substring of that sentence joined together by whitespaces.We are required to write a JavaScript function that takes in a sentence string, str, as the first argument and a number, num, as the second argument. The function should first count the frequency of each word in the sentence and then return an array of length num containing num most frequent words placed according to decreasing frequencies.For example −If the input sentence and the number is −const str ... Read More

Can array be divided into n partitions with equal sums in JavaScript

AmitDiwan
Updated on 27-Feb-2021 17:22:40

173 Views

We are required to write a JavaScript function that takes in an array of numbers, arr, as the first argument and a number, num, as the second argument.The function should determine whether there exists a way of distributing the elements of the array arr into num groups such that all the groups have equal sum. If there exists any such way, our function should return true, false otherwise.For example −If the input array and the number are −const arr = [4, 6, 3, 3, 7, 4, 1]; const num = 4;Then the output should be −const output = true;because the ... Read More

Counting all possible palindromic subsequence within a string in JavaScript

AmitDiwan
Updated on 27-Feb-2021 17:24:49

191 Views

Palindrome Sequence:A string sequence is known as palindrome sequence if it reads the same from front and the back. For instance, 'aba', 'madam, 'did' are all valid palindrome sequences.We are required to write a JavaScript function that takes in a string as the first and the only argument. The string taken as input is guaranteed to consist of only 'a', 'b', 'c' and 'd'. Our function should count and return the number of all contiguous or noncontiguous palindrome subsequences that appear in the string.For example −If the input string is −const str = 'bccb';Then the output should be −const output ... Read More

Find Smallest Letter Greater Than Target in JavaScript

AmitDiwan
Updated on 27-Feb-2021 17:27:05

314 Views

Suppose we are given an array of sorted characters letters containing only lowercase letters. And given a target letter target.We are required to write a JavaScript function that takes in the array as the first argument and the letter as the second. The function is supposed to find the smallest element in the list that is larger than the given target.We have to keep in mind that letters also wrap around. For example, if the target is target = 'z' and letters = ['a', 'b'], the answer is 'a'.For Example −If the input array and letter are −const arr = ... Read More

Advertisements