
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Returning just greater array in JavaScript
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]
- Related Articles
- Smallest prime number just greater than the specified number in JavaScript
- Returning only odd number from array in JavaScript
- Accessing and returning nested array value - JavaScript?
- Returning reverse array of integers using JavaScript
- Returning the highest value from an array in JavaScript
- Returning array values that are not odd in JavaScript
- Returning array of natural numbers between a range in JavaScript
- Returning an array containing last n even numbers from input array in JavaScript
- Next Greater Element in Circular Array in JavaScript
- Returning an array in Java
- JavaScript array.includes inside nested array returning false where as searched name is in array
- Returning an array with the minimum and maximum elements JavaScript
- Returning the first number that equals its index in an array using JavaScript
- Function returning another function in JavaScript
- Sum array of rational numbers and returning the result in simplest form in JavaScript

Advertisements