Found 6683 Articles for Javascript

Returning number of digits in factorial of a number in JavaScript

AmitDiwan
Updated on 22-Apr-2021 10:58:25

109 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 compute and return the number of digits in the factorial of the number num.For example, if the input to the function is −Inputconst num = 7;Outputconst output = 4;Output ExplanationBecause the value of 7! Is 5040 which contains 4 digits.ExampleFollowing is the code − Live Democonst num = 7; const countDigits = (num = 1) => {    let res = 0;    while(num >= 2){       res += Math.log10(num);       num--;    };    return ~~res + 1; } console.log(countDigits(num));Output4

Array index to balance sums in JavaScript

AmitDiwan
Updated on 22-Apr-2021 10:55:13

438 Views

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 required to pick and return one such index from the array such that the sum of elements on its left side is equal to the sum of elements on its right side. If there exists no such index in the array, we should return -1.For example, if the input to the function is −Inputconst arr = [1, 2, 3, 4, 3, 2, 1];Outputconst output = 3;Output ExplanationBecause the sum of elements at either side of ... Read More

Encoding string based on character frequency in JavaScript

AmitDiwan
Updated on 22-Apr-2021 10:50:30

127 Views

ProblemWe are required to write a JavaScript function that takes in a string, str, as the first and the only argument.Our function should create a new string based on the input string where each character in the new string is '(' if that character appears only once in the original string, or ')' if that character appears more than once in the original string.And we should ignore capitaliFor example, if the input to the function is −Inputconst str = 'Success';Outputconst output = ')())())';ExampleFollowing is the code − Live Democonst str = 'Success'; const mapString = (str = '') => {   ... Read More

Limiting elements occurrences to n times in JavaScript

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

160 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

AmitDiwan
Updated on 22-Apr-2021 10:39:16

1K+ Views

ProblemWe are required to write a JavaScript function that takes in an array of exactly 10 positive integers, arr, as the first and the only argument.Our function should then return a string which is in the format of a phone number string.For example, if the input to the function is −Inputconst arr = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0];Outputconst output = '(987) 654-3210';ExampleFollowing is the code − Live Democonst arr = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]; const createNumber = (arr = []) => {    let res = '';    arr = arr.map(String);    res += `(${arr[0]+arr[1]+arr[2]}) `;    res += `${arr[3] + arr[4] + arr[5]}-`;    res += arr[6] + arr[7] + arr[8] + arr[9];    return res; }; console.log(createNumber(arr));Output(987) 654-3210

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

241 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

150 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

334 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

Advertisements