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

476
Views
Returning Null - why is this going wrong?

I am returning null with this code, however I cannot see why/where this is going wrong. (It may be due to the sort function since I got this from a well upvoted snippet on how to sort an array). I care more about the understanding than the correct answer.

Task I am trying to achieve :

Ratiorg got statues of different sizes as a present from CodeMaster for his birthday, each statue having an non-negative integer size. Since he likes to make things perfect, he wants to arrange them from smallest to largest so that each statue will be bigger than the previous one exactly by 1. He may need some additional statues to be able to accomplish that. Help him figure out the minimum number of additional statues needed.

Example

For statues = [6, 2, 3, 8], the output should be solution(statues) = 3.

Ratiorg needs statues of sizes 4, 5 and 7.

My code & thinking:

function solution(statues) {

  let total = 0;

  statues.sort(function(a, b) { //From what I understand this should sort the array numerically 
    return a - b;
  });

  for (let i = 1; i < statues.length; i++) { //iterate through the array comparing the index to the one before it
    if (statues[i + 1] != (statues[i] + 1)) { //if there is a diff between index of more than 1 it will add the diff 
      total += statues[i + 1] - statues[i]; //to the total variable 
    }
  }
  return total;

}

const result = solution([6, 2, 3, 8]);
console.log(result);

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

Thanks to people who commented who helped me see that there were some errors in my logic - I have now changed the if statement to compare an index starting at 1 to the behind it i.e index 1 compared to index 0. On top of that, there was an issue with the following line total += statues[i] - statues[i-1]; I replaced that with a for loop so that it counts up to the number rather than a simple subtraction of it.


 function solution(statues) {
        //[6, 2, 3, 8] --- 2,3,6,8  --- 3,6 7-2, 
        let total = 0;
        
        statues.sort(function(a, b) {                  
            return a - b;
                });
       
        for(let i =1; i<statues.length; i++){         
            if(statues[i] != (statues[i-1]+1) ){       
                //total += statues[i] - statues[i-1];    
                for(let j = statues[i-1]; j<statues[i]-1; j++){
                    total += 1;
                }
                 } 
                 
        }
        return total;
        
    }

about 4 years ago · Santiago Gelvez 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!