Javascript Articles

Page 33 of 534

Changing second half of string number digits to zero using JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 218 Views

ProblemWe are required to write a JavaScript function that takes in a string number as the only argument.Our function should return the input number with the second half of digits changed to 0.In cases where the number has an odd number of digits, the middle digit onwards should be changed to 0.For example −938473 → 938000ExampleFollowing is the code −const num = '938473'; const convertHalf = (num = '') => {    let i = num.toString();    let j = Math.floor(i.length / 2);    if (j * 2 === i.length) {       return parseInt(i.slice(0, j) + '0'.repeat(j));   ...

Read More

Accumulating array elements to form new array in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 308 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers, arr, as the first argument and a number, num, (num {    const res = [];    let sum = 0, right = 0, left = 0;    for(; right < num; right++){       sum += arr[right];    };    res.push(sum);    while(right < arr.length){       sum -= arr[left];       sum += arr[right];       right++;       left++;       res.push(sum);    };    return res; }; console.log(accumulateArray(arr, num));OutputFollowing is the console output−[3, 5, 7, 9, 11]

Read More

Checking for special numbers in JavaScript

Ranjit Kumar
Ranjit Kumar
Updated on 11-Mar-2026 552 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 true if the sum of the digits of the number num is a palindrome number, false otherwise.For example, if the input to the function is −const num = 781296;Then the output should be −const output = true;Output ExplanationBecause the digit sum of 781296 is 33 which is a palindrome number.ExampleFollowing is the code −const num = 781296; const findSum = (num, sum = 0) => { if(num){ return findSum(Math.floor(num / 10), sum + (num % ...

Read More

Merging and rectifying arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 173 Views

ProblemWe are required to write a JavaScript function that takes in two arrays of numbers, arr1 and arr2, as the first and the second arguments.Our function should merge the elements of both these arrays into a new array and if upon merging or before merging there exists any duplicates, we should delete the excess duplicates so that only one copy of each element is present in the merged array.The order here is not so important but the frequency of elements (which should be 1 for each element) is important.For example, if the input to the function is −onst arr1 = ...

Read More

Filtering out numerals from string in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 204 Views

ProblemWe are required to write a JavaScript function that takes in a string, str, which contains a combination of alphabets, special characters and numbers.Our function should return a new string based on the input string that contains only the numbers present in the string str, maintaining their relative order.For example, if the input to the function is −const str = 'revd1fdfdfs2v34fd5gfgfd6gffg7ds';Then the output should be −const output = '1234567';ExampleFollowing is the code −const str = 'revd1fdfdfs2v34fd5gfgfd6gffg7ds'; const pickNumbers = (str = '') => {    let res = '';    for(let i = 0; i < str.length; i++){     ...

Read More

Merging nested arrays to form 1-d array in JavaScript

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

Length of shortest unsorted array in JavaScript

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

Read More

Reshaping 2-D array in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 1K+ 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

Find and return the longest length of set in JavaScript

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

Finding minimum time difference in an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 456 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 −const arr = ["23:59", "00:00"]; const findMinDifference = (arr = []) => {    const find = (str = '') => str.split(':').map(time => parseInt(time, 10))    const mapped = arr.map((time) ...

Read More
Showing 321–330 of 5,338 articles
« Prev 1 31 32 33 34 35 534 Next »
Advertisements