Arrays

Learning Arrays and other features of JS

by habla

HTML

<div id="answer"></div>

JavaScript

a4();
//a3();
/*
function a1()
{
    arr = [1,2,3];
    arr[5] = 6;    
    arr.pop();
    arr.push(new Object({a:1}));
    arr.reverse();
}

function a1a()
{
    arr = [1,2,3,0];
    arr.sort(function(a,b) {
        if (a==2) {return -1;} 
        if (b==2) {return 1;}
        return a-b;
    });
    showobj(arr);
}

function a1b()
{
    arr = [1,2,3].concat([7,8,9]).slice(2,4).join('x');
    showobj(arr);
}

function a2()
{
    var result = 'Martin Capodici'.match(/ic?/g);
    showobj(result);
    
}

function a3()
{
    showobj([1,2,3].length);
}
*/
function a4()
{
    Array.prototype.martin = function(guess){
        if (this.length == guess) { return true; };
        return false;
    }
    
    showobj([1,2].martin(2));
}

function showobj(obj)
{
    $('#answer').text(JSON.stringify(obj));
}