Problem
We 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 first join the numbers present in the array and find the single number represented by the array and then return a new array that represents the number which is greater than the input array number by a magnitude of 1.
For example, if the input to the function is −
Input
const arr = [6, 7, 3, 9];
Output
const output = [6, 7, 4, 0];
Output Explanation
Because the number represented by input array is 6739 and the required number is 6740.
Example
Following is the code −
const arr = [6, 7, 3, 9]; const justGreater = (arr = []) => { if(!arr.every(v=>v>=0) || arr.length === 0){ return null; }; if(arr.some(v=>v.toString().length > 1)){ return null }; let res =[]; for (let i=0; i < arr.length; i += 15){ res.push(arr.slice(i,i+15)); }; res[res.length-1]= res[res.length-1].join('')*1+1 res=res.map(v=>Array.isArray(v)?v.join('')*1:v) return (res.join('')).split('').map(v=>v*1) }; console.log(justGreater(arr));
Output
[6, 7, 4, 0]