Brett Kessler
Brett Kessler

Reputation: 71

Could someone explain how this function works in laymans terms?

So I know what this code is doing, it is checking to see if the code is "uniform" for instance a,a,a,a,a,a or 1,1,1,1,1,1 and if it's not it is returning a false statement. However I'm not exactly sure how this equation is working. Mostly I'm confused by the array[0] and why the variable is i = 1. Could someone walk me through exactly what is happening here and why this works?

function isUniform(arr){
    var first = arr[0];
    for(var i = 1; i < arr.length; i++){
        if(arr[i] !== first){
            return false;
        }
    }
    return true;
}

Upvotes: 0

Views: 123

Answers (4)

garethb
garethb

Reputation: 4041

length in javascript is not 0 based. So the following array has a length of 4.

[1,2,3,4]

Arrays on the other hand are 0 based, so to get the 4th element of this array you would do arr[3] which would equal 4. The number inside the square braces represents the position in the array, not the value.

Upvotes: 0

ibrahim mahrir
ibrahim mahrir

Reputation: 31712

First, we stores the first element of the array into the variable first. and then we start looping over the rest of the array's elements (that's why i starts with 1 because there is no need to compare an element with itself). If the current element arr[i] is different from the first element first (means the array is not uniform), the function return false (when a function return it will be exited immediately so there will be no more comparing with other elements of the array). If we loop over all the elements of the array and we came out of the loop then we didn't find any different values, so the array is uniform thus we return true and exit the function.

Upvotes: 0

j08691
j08691

Reputation: 207943

Arrays in JavaScript are zero-based. Therefore the line

var first = arr[0]

represents the first element of the arr argument passed into the array. It never changes.

The loop

for(var i = 1; i < arr.length; i++)

iterates over all but the first element array, starting at the second element (arr[1]) to the final element. Along the way the statement if(arr[i] !== first) simply compares each array element (except for the first one), to the first array element being stored in first. If any one doesn't match, false is returned, else true is returned. If the input array was [1,2,3,4,5], then arr[0]=1, arr[1]=2, arr[2]=3... and the value of the first array element (and the variable first) would be 1, and the loop would iterate over the rest of the array values (2, 3, 4, and 5). It would therefore return false on the first iteration as 1 is not equal to 2.

There's nothing fancy or exceptional about this function as all it does is walk down the values of an array, comparing elements.

Upvotes: 1

Keerthi Kumar P
Keerthi Kumar P

Reputation: 1604

Have given comments in the code using // hope it helps

function isUniform(arr){
    var first = arr[0]; //storing the first value in the array in variable 'first'
   //arr.length gives the number of elements in the array
   //looping between numbers 1 and the end of the length of array (array.length)
    for(var i = 1; i < arr.length; i++){ 
        if(arr[i] !== first){ //checking if current value (arr[i]) is equal to first value
            return false;//if it is not equal returning false and return ends the loop and function
        }
          //if all the values are equal the above if condition never runs
    }
    //if above if condition never runs, this statement runs and returns true
    return true; 
}

Upvotes: 2

Related Questions