Samiksha Jagtap
Samiksha Jagtap

Reputation: 603

arrange numbers in array such that it creates biggest number

Given an array of numbers, arrange them in a way that yields the largest value. For example, if the given numbers are {54, 546, 548, 60}, the arrangement 6054854654 gives the largest value.

var maxCombine = (a) => +(a.sort((x, y) => +("" + y + x) - +("" + x + y)).join(''));
var b = [54, 546, 548, 60];
// test & output
console.log([
  b
].map(maxCombine));

It works properly and prints answer on log. but when i use it in view it does not render anything

return (
  <div>{[b].map(maxCombine))}</div>
) 

Upvotes: 1

Views: 908

Answers (1)

Mayank Shukla
Mayank Shukla

Reputation: 104379

It should work, In your case output will a single element in array.

Try this:

return (
   <div>{([b].map(maxCombine))[0]}</div>
) 

Or use this:

return (
  <div>{JSON.stringify([b].map(maxCombine))}</div>
) 

Check this snippet, output will be an array of one element:

var maxCombine = (a) => +(a.sort((x, y) => +("" + y + x) - +("" + x + y)).join(''));
var b = [54, 546, 548, 60];

console.log([
  b
].map(maxCombine));

Upvotes: 1

Related Questions