Sum all the numbers

hey guys, can anyone clear this out:

This is the correct way:

function sumAll(arr) {
  var min = Math.min(arr[0], arr[1]);
  var max = Math.max(arr[1], arr[0]);
  var total = 0;
for(var i=min; i<=max; i++){
  total += i; } 

  return total; 

}

sumAll([1, 4]);

This is FALSE:

function sumAll(arr) {
  var min = Math.min(1, 4);
  var max = Math.max(1, 4);
  var total = 0;
for(var i=min; i<=max; i++){
  total += i; } 

  return total; 

}

sumAll([1, 4]);

How come?

The second snippet hard-codes 1 and 4 as the minimum and maximum values. The values that you plug in to Math.min and Math.max should come from the array that you pass to the function. By hard-coding the values, the function will only ever sum the numbers from 1 to 4. The array is entirely ignored.

Just to add, if the SumAll function is passed the array [1, 4] it will appear to work for that array but it is working exactly as kevcomedia said. :beers:

1 Like

same goes here:

function translatePigLatin(str) {

var first = str.match(/[aeiou]/);
var firstPosition = str.indexOf(first);

if(firstPosition > 0) {
return str.slice(firstPosition) + str.slice(0, firstPosition) + "ay"; }
  return str + "way";
}

translatePigLatin("consonant");

as you can see "str.slice(0, firstPosition) <-- it does recognize β€œ0” which is equal to β€œC” but if i change to β€œstr.slice(0, 1)” it will be completely lost, since he will not recognize the β€œ1” which is β€œo”… This is pretty weird to me