Javascript Array head()

Description

Javascript Array head()



Array.prototype.head = function(){
    return this[0];
}

Array.prototype.tail = function(){
    return this.slice(1);
}

var a = [1,2,3,4,5,6];


function sumaArr(arr){
    if ( arr.length === 0 ) { 
        return 0;
    }// ww  w  .ja v a2  s  .c  o  m
    return arr.head() + sumaArr(arr.tail());
}

console.log(a.head());
console.log(a.tail());
console.log(a);
console.log(sumaArr(a));

Javascript Array head()

Array.prototype.head = function () {
    if (this.length == 0) {
        return null;
    }//from  ww w.  jav a2  s  . c o m
    return this[0];
}



PreviousNext

Related