Found 10710 Articles for Web Development

Sorting numbers based on their digit sums in JavaScript

AmitDiwan
Updated on 22-Apr-2021 10:36:28

448 Views

ProblemWe are required to write a JavaScript function that takes in an array of positive integers, arr, as the first and the only argument.Our function should sort the input array in such a way that the number that have the highest digit sum comes first followed by the numbers with lesser digit sums.For example, if the input to the function is −Inputconst arr = [5, 34, 1, 13, 76, 8, 78, 101, 57, 565];Outputconst output = [565, 78, 76, 57, 8, 34, 5, 13, 101, 1];Output ExplanationBecause 565 have the highest digit sum of 16, followed by 78 and 76 ... Read More

Removing parentheses from mathematical expressions in JavaScript

AmitDiwan
Updated on 22-Apr-2021 10:28:19

242 Views

ProblemWe are required to write a JavaScript function that takes in a string of mathematical expressions, str, as the first and the only argument.The task of our function is to remove parentheses from the expression keeping the operations and operands in place.For example, if the input to the function is −Inputconst str = 'u-(v-w-(x+y))-z';Outputconst output = 'u-v+w+x+y-z';ExampleFollowing is the code − Live Democonst str = 'u-(v-w-(x+y))-z'; const removeParentheses = (str = '') => {    let stack = []    let lastSign = '+'       for (let char of str) {          if (char === '(' ... Read More

Counting steps to make number palindrome in JavaScript

AmitDiwan
Updated on 22-Apr-2021 10:20:07

151 Views

ProblemWe are required to write a JavaScript function that takes in a number, num, as the first and the only argument.Our function should return the number of special steps needed to obtain a palindrome. The special step is: "reverse the digits, and add to the original number". If the resulting number is not a palindrome, repeat the procedure with the sum until the resulting number is a palindrome.For example, if the input to the function is −Inputconst num = 87;Outputconst output = 4;Output ExplanationBecause the steps involved are −87 + 78 = 165; 165 + 561 = 726; 726 + ... Read More

Using Kadane’s algorithm to find maximum sum of subarray in JavaScript

Aayush Mohan Sinha
Updated on 04-Aug-2023 10:41:28

340 Views

Exploring sophisticated algorithms is key to unlocking the true potential of JavaScript programming, particularly when it comes to solving complex computational challenges. Among these algorithms, Kadane's algorithm stands out as a formidable tool for efficiently finding the maximum sum of subarrays within an array. This remarkable technique allows developers to optimize their code and enhance the performance of their applications when dealing with large data sets. In this article, we delve into the intricacies of Kadane's algorithm, unveiling its inner workings and showcasing its efficacy in tackling the task of identifying the maximum sum of subarrays in JavaScript. By grasping ... Read More

Representing seconds in HH:MM:SS in JavaScript

Aayush Mohan Sinha
Updated on 04-Aug-2023 10:29:41

1K+ Views

The representation of seconds in the format of HH:MM:SS holds great significance in various domains, as it enables precise time tracking and measurement in JavaScript applications. Mastering the art of converting raw seconds into a visually comprehensible HH:MM:SS format is a valuable skill for developers seeking to present time-based data with utmost accuracy and clarity. In this article, we delve into the intricacies of representing seconds in the HH:MM:SS format using JavaScript, exploring the underlying algorithms and methodologies that empower developers to elegantly display time intervals in a human-readable manner. Problem Statement Given a number representing the duration in seconds, ... Read More

Finding the final direction of movement in JavaScript

AmitDiwan
Updated on 22-Apr-2021 09:54:02

249 Views

ProblemWe are required to write a JavaScript function that takes in an array of single characters, arr, as the first and the only argument.The array can contain of only 4 characters, they are −‘N’ → stands for North direction‘S’ → stands for South direction‘W’ → stands for West direction‘E’ → stands for East directionEach character specifies a move of unit distance in that particular direction. And if anywhere in the array, two opposite directions, [(‘S’ and ‘N’) or (‘E’ and ‘W’)] appear adjacently, they cancel out the movement of each other. Therefore, our function is supposed to find the resulting ... Read More

Breaking camelCase syntax in JavaScript

AmitDiwan
Updated on 22-Apr-2021 10:09:45

174 Views

ProblemWe are required to write a JavaScript function that takes in a camelCase string, str as the first and the only argument.Our function should construct and return a new string that splits the input string using a space between words.For example, if the input to the function is −Inputconst str = 'thisIsACamelCasedString';Outputconst output = 'this Is A Camel Cased String';ExampleFollowing is the code − Live Democonst str = 'thisIsACamelCasedString'; const breakCamelCase = (str = '') => {    const isUpper = (char = '') => char.toLowerCase() !== char.toUpperCase() && char === char.toUpperCase();    let res = '';    const { length: ... Read More

Removing comments from array of string in JavaScript

AmitDiwan
Updated on 21-Apr-2021 13:17:35

254 Views

ProblemWe are required to write a JavaScript function that takes in array of strings, arr, as the first argument and an array of special characters, starters, as the second argument.The starter array contains characters that can start a comment. Our function should iterate through the array arr and remove all the comments contained in the strings.For example, if the input to the function is:const arr = [    'red, green !blue',    'jasmine, #pink, cyan' ]; const starters = ['!', '#'];Then the output should be −const output= [    'red, green',    'jasmine, ' ];ExampleFollowing is the code − Live Democonst ... Read More

Finding all peaks and their positions in an array in JavaScript

AmitDiwan
Updated on 21-Apr-2021 13:13:06

439 Views

Build UpSuppose we have the following array in JavaScript −const arr = [4, 3, 4, 7, 5, 2, 3, 4, 3, 2, 3, 4];If we plot the points of this array on y-axis with each adjacent point being unit distance away on x-axis, the graph will look like this −This graph clearly shows that there exist two local maxima (peaks) in this array at index 3 and 7 with values 7 and 4 respectively.ProblemWe are required to write a JavaScript function that takes in an array of integers, arr, as the first and the only argument.Our function is supposed to ... Read More

Finding the just bigger number formed by same digits in JavaScript

AmitDiwan
Updated on 21-Apr-2021 12:58:39

47 Views

ProblemWe are required to write a JavaScript function that takes in a number, num, as the first and the only argument.Our function should find and return a number such that it contains only and all the digits of the input number and is just bigger than the input numberIf there exists no such number, our function should return -1.For example, if the input to the function is −const num = 5656;Then the output should be −const output = 5665;Output ExplanationBecause 5665 contains only and all digits of 5656 and is just greater than 5656.ExampleFollowing is the code &mius; Live Democonst num ... Read More

Advertisements