Sum all the numbers

Sum all the numbers
0.0 0

#1

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?


#2

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.


#3

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:


#4

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