Javascript Articles

Page 34 of 534

Creating a chained operation class in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 149 Views

ProblemWe are supposed to create a user defined data type Streak in JavaScript that can be chained to any extent with value and operations alternativelyThe value can be one of the following strings −→ one, two three, four, five, six, seven, eight, nineThe operation can be one of the following strings −→ plus, minusFor example, if we implement the following in context of our class −Streak.one.plus.five.minus.three;Then the output should be −const output = 3;Output ExplanationBecause the operations that took place are −1 + 5 - 3 = 3ExampleFollowing is the code −const Streak = function() {    let value = 0;   ...

Read More

Finding all peaks and their positions in an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 660 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

Removing comments from array of string in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 390 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 −const arr ...

Read More

Breaking camelCase syntax in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 297 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 −const str = 'thisIsACamelCasedString'; const breakCamelCase = (str = '') => {    const isUpper = (char = '') => char.toLowerCase() !== char.toUpperCase() && char === char.toUpperCase();    let res = '';    const { length: len ...

Read More

Finding the final direction of movement in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 384 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

Counting steps to make number palindrome in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 250 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

Removing parentheses from mathematical expressions in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 374 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 −const str = 'u-(v-w-(x+y))-z'; const removeParentheses = (str = '') => {    let stack = []    let lastSign = '+'       for (let char of str) {          if (char === '(' || ...

Read More

Sorting numbers based on their digit sums in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 608 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

Limiting elements occurrences to n times in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 275 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

Encoding string based on character frequency in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 250 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 −const str = 'Success'; const mapString = (str = '') => {    const ...

Read More
Showing 331–340 of 5,338 articles
« Prev 1 32 33 34 35 36 534 Next »
Advertisements