Found 10710 Articles for Web Development

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

306 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

Find and return the longest length of set in JavaScript

AmitDiwan
Updated on 21-Apr-2021 12:32:33

151 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers, arr, as the first and the only argument.The array, arr, of length N contains all integers from 0 to N-1. Our function is supposed to find and return the longest length of set S, where S[i] = {A[i], A[A[i]], A[A[A[i]]], ... } subjected to the rule below.Suppose the first element in S starts with the selection of element A[i] of index = i, the next element in S should be A[A[i]], and then A[A[A[i]]]… By that analogy, we stop adding right before a duplicate element ... Read More

Reshaping 2-D array in JavaScript

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

736 Views

ProblemWe are required to write a JavaScript function that takes in a 2-D array of numbers, arr, as the first argument and two numbers, r and c, representing the row number and column number of the desired matrix, respectively.Our function should form and return a new 2-D array with the specified rows and columns in the same row-traversing order as they were in the input array.For example, if the input to the function is −const arr = [    [6, 7],    [8, 9] ]; const r = 1, c = 4;Then the output should be −const output = [[6, 7, 8, ... Read More

Length of shortest unsorted array in JavaScript

AmitDiwan
Updated on 21-Apr-2021 12:20:05

77 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers, arr, as the first and the only argument.Our function needs to find the length of one continuous subarray such that if we only sort this subarray in ascending order, then the whole array will be sorted in ascending order, too.For example, if the input to the function is −const arr = [3, 7, 5, 9, 11, 10, 16];Then the output should be −const output = 5;Output ExplanationBecause if we sort [7, 5, 9, 11, 10], the whole array will be sorted.ExampleFollowing is the code − Live ... Read More

Longest subarray with unit difference in JavaScript

AmitDiwan
Updated on 21-Apr-2021 12:17:48

65 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers, arr, as the first and the only argumentOur function should find and return the length of such a subarray where the difference between its maximum value and its minimum value is exactly 1.For example, if the input to the function is −const arr = [2, 4, 3, 3, 6, 3, 4, 8];Then the output should be −const output = 5;Output ExplanationBecause the desired subarray is [4, 3, 3, 3, 4]ExampleFollowing is the code −const arr = [2, 4, 3, 3, 6, 3, 4, 8]; const ... Read More

Merging nested arrays to form 1-d array in JavaScript

AmitDiwan
Updated on 21-Apr-2021 12:08:05

244 Views

ProblemWe are required to write a JavaScript function that takes in two nested arrays, arr1 and arr2, as the first and the second argument.Our function should create and return a third array that contains all the elements of arr1 and arr2 but flattened to single dimensionFor example, if the input to the function is −const arr1 = [    1, [       2, [          4, 5, [             6          ]       ]    ] ]; const arr2 = [    11, 12, [ ... Read More

Advertisements