0

I am writing this code to find missing numbers from a given array. This code works fine when i pass 1,4 as arguments but 5,10 it fails to push new items to the array. What am I doing wrong?

 function sumAll(arr) {
   max = Math.max(...arr);
   min = Math.min(...arr);
   toFill = max - min;
   for (i = min + 1; i <= toFill; i++) {
     arr.push(i);
   }
   return arr.sort().reduce((prev, curr) => prev + curr);

 }

 sumAll([5, 10]);

3
  • I think i immediately found my problem. if my min is equal to my 'toFill' my for loop will not run... makes sense now. Will look into it further and post an update. Commented Jun 13, 2016 at 15:22
  • just delete the question, if you found your own solution. Commented Jun 13, 2016 at 15:26
  • 1
    I didnt find the solution, i found a problem that may help find the solution. Commented Jun 13, 2016 at 15:35

1 Answer 1

1

You need to say i <= min+toFill

function sumAll(arr) {
   max = Math.max(...arr);
   min = Math.min(...arr);
   toFill = max - min;
   for (i = min + 1; i <= min+toFill; i++) { console.log(i);
     arr.push(i);
   }
   return arr.sort().reduce((prev, curr) => prev + curr);

 }
Sign up to request clarification or add additional context in comments.

1 Comment

I had to get rid of += in my for loop. for(i = min +1; i < min+toFill; i++)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.