Reputation: 447
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?
Upvotes: 2
Views: 400
Reputation: 15509
Pass in the number into the function, split the number into individual digits, convert to an array of integers, sort by descending order and then join the items together to give the returned number.
let numbers =[9,0,1,43,81,21,20,91,32,53,473,850,801,100,219,581,12345,12345000,1010100,8001009100]
numbers.forEach(function(number){
console.log(myFunction(number));
})
// gives 9,0,1,43,81,21,0,91,32,53,743,850,81,100,921,851,54321,54321000,1110000,9811000000
function myFunction(num) {
let numArray= num.toString().split('');
numArray.forEach(function(num){
num = parseInt(num);
})
numArray.sort(function(a, b){return b - a});
return numArray.join('');
}
Upvotes: 1
Reputation: 92440
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))
Upvotes: 1
Reputation: 4768
/*
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;
*/
const sortFunc = (a, b) => {
if(a < b) return 1;
if(a > b) return -1;
return 0;
}
function largestNumber(num) {
return (num).toString()
.split('')
.map(e => parseInt(e))
.sort(sortFunc);
}
console.log(largestNumber(234));
console.log(largestNumber(184507609));
Upvotes: 0