Search code examples
javascriptsortingnumbers

JS How to take an array of numbers and return the largest passible combined number


I am new to coding and have been given this question but am net sure how to fix my code to make it work.

The question I have been given is this;

Create a function that takes a number and returns the largest number that can be made with the same digits.

  E.g. if num is 23, the function should return 32.
  E.g. if num is 9, the function should return 9.
  E.g. if num is 581 the function should return 851.

The code that I have written is this;

function largestNumber(num) {
nums.sort(a, b) 
{
a + b
}
return nums.toString()
};

The test that me code is being run against is this;

describe("largestNumber", () => {
  it("if passed a single-digit number then returns that number", () => {
    expect(largestNumber(9)).to.equal(9);
    expect(largestNumber(0)).to.equal(0);
    expect(largestNumber(1)).to.equal(1);
  });
  it("if passed a 2-digit number then does nothing if they are in descending order", () => {
    expect(largestNumber(43)).to.equal(43);
    expect(largestNumber(81)).to.equal(81);
   expect(largestNumber(21)).to.equal(21);
   expect(largestNumber(20)).to.equal(20);
 });
 it("if passed a 2-digit number then swaps the numbers if they are are in ascending order", () => {
   expect(largestNumber(19)).to.equal(91);
   expect(largestNumber(23)).to.equal(32);
   expect(largestNumber(35)).to.equal(53);
 });
 it("if passed a 3-digit number then returns the correctly ordered number", () => {
   expect(largestNumber(473)).to.equal(743);
   expect(largestNumber(850)).to.equal(850);
   expect(largestNumber(801)).to.equal(810);
   expect(largestNumber(100)).to.equal(100);
   expect(largestNumber(219)).to.equal(921);
   expect(largestNumber(581)).to.equal(851);
 });
 it("returns correctly ordered number for large numbers including those with many trailiing zeros", () => {
   expect(largestNumber(12345)).to.equal(54321);
   expect(largestNumber(12345000)).to.equal(54321000);
   expect(largestNumber(1010100)).to.equal(1110000);
   expect(largestNumber(89382291)).to.equal(99883221);
   expect(largestNumber(8001009100)).to.equal(9811000000);
 });
});

Dose anyone have any suggestion of how to make my code run correctly?


Solution

  • It's off a bit from the start. You function takes an integer as an argument. This mean you can't just use nums.sort() because integers don't have a sort() method and also because what you really want to do is sort the individual digits. In javascript the most convenient way to do this is by turning into a string and then splitting the string into digits.

    For example:

    let n = 5713
    let arr = n.toString().split('')
    
    console.log(arr)

    That is something you can now sort. After you you've sorted it, you can put it back together with join() and then turn it back into a number with parseInt():

    let arr = ["7", "5", "3", "1"]
    let str = arr.join('')
    let n = parseInt(str)
    
    console.log(n)

    You can set the list even if it's a list of strings. With something like:

    list.sort((a, b) => b - a) // the - will convert the strings to numbers for you
    

    b-a will return a negative number when a > b, a positive when a < b and zero when they are the same. That's exactly what you want for the sort() callback and it's a ubiquitous pattern in javascript.

    In the end you might end up with something like:

    function largestNumber(num) {
      let str = num.toString().split('')  // turn number into string and spilt into array
      .sort((a, b) => b - a)              // reverse sort
      .join('')                           // join back to a string
    
      return parseInt(str)                // return a number
      };
    
     console.log( largestNumber(5371))