Return the sum of the two lowest positive numbers in that array

Hello Guys,

Just wanted to ask a question regarding this topic. On my first method i have an array of numbers and I’m looping/iterate the array of numbers., then grab the two smallest numbers and add them, to return their adding value of the two values. (This works well.)

Now. I wanted to do this with new sort() and splice() Methods ES6 syntax, but i wanted to check with you guys if my method 2 its another way of accomplish this. and if I’m doing it right for best practices.

I have attach code pen just in case somone need to do testing or work on the code adjustments.

https://codepen.io/ivanmt07/pen/MWeMBXx?editors=1012

Can you let me know , i appreciate this. Kind Regards.

Method 1: Good old vanilla js with for loop.

var dogQWalks = [19, 5, 42, 2, 77];

dogQWalks.sort(function(a, b) {
  return a - b;
});

for( var i = 0; i < dogQWalks.length; i= i + 1 ){
    var total = 0;
    total = total + dogQWalks[i];
     
}

const sumTwoSmallestNumbers = (numbers) => {
  //Code here
  let ordered = numbers.sort(function(a,b){return a-b;});
//  console.log(ordered);
  let result = 0;
    for (let i = 0; i < ordered.length; i++){
      if (i===0){
        result = result + ordered[0]; //2
        console.log(result);
      }
      if (i===1){
        result = result + ordered[1]; //5
        console.log(result);
      }
    }
    return result;
};

console.log(sumTwoSmallestNumbers([19, 5, 42, 2, 77]));
//Output:  (2 + 5) =  7

Method 2: Using sort() and Splice () Method.

function addLowerNum(dogQWalks){
  
  var orderNumbers = dogQWalks.sort(function(a, b) {  
   return a - b;
 });
  //console.log(orderNumbers); 
  var loweNumbers = orderNumbers.splice(0,2);
  //console.log(loweNumbers);
  var total = 0;
  for( var i = 0; i < loweNumbers.length; i++ ){
    total = total + loweNumbers[i];
  }
  return total;
  //console.log(total);
  
};

console.log(addLowerNum([19, 5, 42, 2, 77]));
//Output:  (2 + 5) =  7

I won’t comment regarding best practices or not, but gonna say to look at something slightly different.

Notice that sorting part is essentially the same. Main difference is that in first one whole ordered array is iterated and in the second just the whole spliced part that matters for the function.

Keeping in mind that it’s known where in the sorted array are lowest numbers and which numbers won’t matter, think whether this knowledge can be leveraged in the iteration and adding part. Not necessarily by creating separate array consisting just part of the numbers.

1 Like

Thank you for the feedback, and I’m well aware that sort() its the same for both … just that like you said in the first method will break with an iterate, and in the second method we do the spliced() thank you for a better understanding about the two methods,

Would you do something similar or different with new ES6 maybe not the function arrow but the methods ?

Thank you so much.

In method 1 you’re doing unnecessary looping after i > 1. You should either change the exit condition or explicitly break for loop when i > 1.

In method two splice doesn’t add anything to the code. You could achieve the same result by limiting for loop to i < 2.
If you want to take advantage of declarative programming, reduce or forEach would be a better fit instead of for loop.

1 Like

My point is that in the first case it’s also possible to iterate just over the relevant numbers, not whole array, without splicing them explicitly. And in process simplify the contents of for loop. It’s just needs realization that those two relevant numbers always will be in the certain (and the same) place in the sorted array.

1 Like

Awesome @jenovs thanks for the feedback I’m surely will look into this typos and try to work on them. yes for Each will be an option to reduce code. I will work on those alternatives.

Thanks.

Alright thank you so much, for all your feedback i will work on those corrections and try to make it better and readable to understand.

Thank you so much.