Found 6683 Articles for Javascript

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

Converting seconds in years days hours and minutes in JavaScript

AmitDiwan
Updated on 21-Apr-2021 12:51:45

1K+ Views

ProblemWe are required to write a JavaScript function that takes in a number, num, that represents the number of seconds as the first and the only argument.Our function should then construct and return a string that contains information of years, days, hours and minutes contained in those seconds, obviously if contained at all.For the purpose of this question, we consider that all the years have 365 daysFor example, if the input to the function is −const num = 5454776657;Then the output should be −const output = '172 years, 353 days, 23 hours, 44 minutes and 17 seconds';ExampleFollowing is the code ... Read More

Creating a chained operation class in JavaScript

AmitDiwan
Updated on 21-Apr-2021 12:47:52

73 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 − Live Democonst Streak = function() {    let value = 0; ... Read More

Finding minimum time difference in an array in JavaScript

AmitDiwan
Updated on 21-Apr-2021 12:41:47

305 Views

ProblemWe are required to write a JavaScript function that takes in an array of 24-hour clock time points in "Hour:Minutes" format. Our function should find the minimum minutes difference between any two time points in the array.For example, if the input to the function is −const arr = ["23:59", "00:00"];Then the output should be −const output = 1;Because the minimum difference between the times is 1 minuteExampleFollowing is the code − Live Democonst arr = ["23:59", "00:00"]; const findMinDifference = (arr = []) => {    const find = (str = '') => str.split(':').map(time => parseInt(time, 10))    const mapped = ... Read More

Reversing and preserving spaces in JavaScript

AmitDiwan
Updated on 21-Apr-2021 12:37:24

257 Views

ProblemWe are required to write a JavaScript function that takes in a sentence string, str, as the first and the only argument.Our function is supposed to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.For example, if the input to the function is −const str = 'this is some sample string';Then the output should be −const output = 'siht si emos elpmas gnirts';ExampleFollowing is the code − Live Democonst str = 'this is some sample string'; const reverseWords = (str = '') => {    return str.trim()       .split(/\s+/) ... Read More

Achieving maximum possible pair sum in JavaScript

AmitDiwan
Updated on 21-Apr-2021 12:34:27

171 Views

ProblemWe are required to write a JavaScript function that takes in an array of integers, arr, which is of length 2n as the first and the only argument.The task of our function is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), ..., (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as large as possible.For example, if the input to the function is −const arr = [1, 4, 3, 2];Then the output should be −const output = 4;Output Explanationn is 2, and the maximum sum of pairs is ... Read More

Advertisements