Found 10483 Articles for Web Development

Limiting elements occurrences to n times in JavaScript

AmitDiwan
Updated on 22-Apr-2021 10:44:53

238 Views

ProblemWe are required to write a JavaScript function that takes in an array of integers, arr, that may contain duplicates as the first argument, and a number, num, as the second and final argument.The task of our function is to iterate through the array and check whether there exists some number that appears for more than n times in the array.If there exists any such element, we should delete its extra occurrences to limit its occurrence to at most num.For example, if the input to the function is −Inputconst arr = [4, 1, 3, 1, 4, 1, 3, 4, 2]; ... Read More

Converting array to phone number string in JavaScript

Revathi Satya Kondra
Updated on 30-Jan-2025 17:48:53

1K+ Views

To convert an array to a phone number string in JavaScript can be done by formatting the array elements into the desired phone number pattern. Following are the steps to learn how to convert array to phone number string in Javascript − Ensure that the array has the correct number of elements (usually 10 for a standard phone number). Join the elements of the array into a single string. Format the string into the desired phone number pattern, such as (XXX) XXX-XXXX. Let us understand through ... Read More

Sorting numbers based on their digit sums in JavaScript

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

559 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

327 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

210 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

589 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

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

347 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

258 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

335 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

Advertisements