Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

191
Views
javascript query: somehow javascript is finding length of an integer

unsorted = [2,4,88,5,6,5,5,8888888]
unsorted = unsorted.sort(function(a,b){
   if (a.length == b.length) {
        if (a < b) {
            return -1;
        } else if (a > b) {
            return 1;
        } else {
            return 0;
        }
        
    } else if (a.length < b.length) {
        return -1;
    } else {
        return 1;
    }
});
console.log(unsorted);
explain the compare function used to sort array in ascending order and while using compare function as (a,b)=>a-b, some of the test cases didn't pass.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Numbers do not have a length property, so 1.length is undefined. Since a.length and b.length are both undefined, a.length == b.length returns true.

If you'd actually like to compare the length of digits in a number, first convert it to a string: a.toString().length

var a=1;
var b=10;
console.log("a.length: " + a.length);
console.log("b.length: " + b.length);
console.log(a.length == b.length);


console.log("a.toString().length: " + a.toString().length);
console.log("b.toString().length: " + b.toString().length);
console.log(a.toString().length == b.toString().length);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!