Archive for August, 2012

August 27, 2012

ASP.NET MVC3 with Razor view engine using Monodevelop and Ubuntu 12.04

Developing .Net web applications in a linux environment has been somewhat of a personal curiosity for quite some time. I have Ubuntu 12.04 installed in a dual boot configuration and every once in a while get an urge to actually boot Ubuntu and tinker with Monodevelop to see how far along the project has come. Since most of my time is spent developing ASP.NET MVC3 applications, I decided to see if it was possible to get a simple application running using the Razor view engine.

The last time I attempted this (over a year ago), it turned out to be more of a pain getting a web server configured to run ASP.NET applications than it was worth. The experience this time was much better as Monodevelop has xsp4 integrated out of the box for serving the web pages. xsp4 is a minimalistic web server which hosts the ASP.NET runtime.

To be honest, I was hoping that by now Monodevelop would have support for the Razor view engine and it would just work ‘out of the box’. This of course was just wishful thinking. However, the actual process to get it working isn’t a deal breaker anymore; especially after you have done it once.

To begin with, you will want to get the latest version of Monodevelop. The Ubuntu repository is a bit behind, so using a ppa is your best bet:

sudo add-apt-repository ppa:keks9n/monodevelop-latest
sudo apt-get update
sudo apt-get install monodevelop

This process will take a few minutes depending on your connection speed. Once installed, launch Monodevelop and click Start New Solution. Select ASP.NET MVC Project from the C# section and give your project a name and location.

Once the project is created, compile it and optionally run it. Compiling the project will create a bin folder with the relevant assemblies. If your project will not compile and the message refers to a .NET 3.5 compiler not being found, be sure to change your build to use .NET 4.0. Right click on your project (not the solution) and select options. Under build, click on ‘General’ and select ‘Mono/.NET 4.0’.

In the bin folder, there will be a System.Web.Mvc.dll. This is an ‘old’ version and will be replaced. The first thing to do at this point is remove that reference from your project. In the solution explorer of Monodevelop, expand the references and then right-click delete System.Web.Mvc.

The next steps require that you have some assemblies from a Windows compiled MVC 3 project. If you don’t have access to a Windows machine, just google for them. The assemblies that you will want to copy over to the bin folder are as follows:

  • System.Web.Helpers.dll
  • System.Web.Mvc.dll
  • System.Web.Razor.dll
  • System.Web.WebPages.dll
  • System.Web.WebPages.Deployment.dll
  • System.Web.WebPages.Razor.dll
  • Once you have copied them to the bin folder, add them as references to your project. To do this, right click ‘references’ in the solution explorer and select ‘edit references’. Click the .Net Assembly tab and double click the bin folder. Control-click all of the above dll’s and then click the ‘add’ button.

    Almost there. The project that was created by Monodevelop is using aspx pages. You will need to configure the project to use the Razor view engine. You can manually edit the Web.config to include razor, or be lazy like I was and just copy everything from a project created in Visual Studio. Here’s a complete Web.Config that you can copy and paste:

    <?xml version="1.0"?>
    
    <configuration>
      <configSections>
        <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
          <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
          <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
        </sectionGroup>
      </configSections>
    
      <system.web.webPages.razor>
        <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <pages pageBaseType="System.Web.Mvc.WebViewPage">
          <namespaces>
            <add namespace="System.Web.Mvc" />
            <add namespace="System.Web.Mvc.Ajax" />
            <add namespace="System.Web.Mvc.Html" />
            <add namespace="System.Web.Routing" />
          </namespaces>
        </pages>
      </system.web.webPages.razor>
    
      <appSettings>
        <add key="webpages:Enabled" value="false" />
      </appSettings>
    
      <system.web>
        <httpHandlers>
          <add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
        </httpHandlers>
    
        <!--
            Enabling request validation in view pages would cause validation to occur
            after the input has already been processed by the controller. By default
            MVC performs request validation before a controller processes the input.
            To change this behavior apply the ValidateInputAttribute to a
            controller or action.
        -->
        <pages
            validateRequest="false"
            pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
            pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
            userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
          <controls>
            <add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
          </controls>
        </pages>
      </system.web>
    
      <system.webServer>
        <validation validateIntegratedModeConfiguration="false" />
    
        <handlers>
          <remove name="BlockViewHandler"/>
          <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
        </handlers>
      </system.webServer>
    </configuration>
    

    It’s probably a good idea to copy Global.asax.cs for updated route configuration. This is the step where you will be glad to have copied System.Web.Mvc from a Windows build. Without it, you will not be able to compile the project because GlobalFilterCollection will not exist. Replace the complete class with the following:

        public class MvcApplication : System.Web.HttpApplication
        {
            public static void RegisterGlobalFilters(GlobalFilterCollection filters)
            {
                filters.Add(new HandleErrorAttribute());
            }
    
            public static void RegisterRoutes(RouteCollection routes)
            {
                routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
                routes.MapRoute(
                    "Default", // Route name
                    "{controller}/{action}/{id}", // URL with parameters
                    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
                );
    
            }
    
            protected void Application_Start()
            {
                AreaRegistration.RegisterAllAreas();
    
                RegisterGlobalFilters(GlobalFilters.Filters);
                RegisterRoutes(RouteTable.Routes);
            }
        }
    

    Finally, rename ~/Views/Home/Index.aspx to Index.cshtml and then replace its contents with the following:

    @{
        ViewBag.Title = "Home Page";
    }
    
    <h2>@ViewBag.Message</h2>
    <p>
        To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>
    </p>
    

    If all went well, you can compile and run your project by hitting F5.

    Advertisement
    August 21, 2012

    Introduction to data structures and algorithms in javascript: Binary Search Tree

    This is a continuation of a series introducing data structures in javascript. A binary tree is a tree
    based data structure in which each node has at most two child nodes. The child nodes are typically
    referred to as the left and right nodes. Nodes with children are called parent nodes. The absolute
    first node in the tree is referred to as the root node.

    A binary search tree is a binary tree that is organized with the following properties:

    – The left subtree of a node contains only nodes with keys that are less than the nodes key.
    – The right subtree of a node contains only nodes with keys that are greater than the nodes key.
    – Both the left and right subtrees must also be binary trees

    With the data inserted into the tree in this manner, searching becomes more effecient than within an array
    because traversal of the data structure can logically exclude elements for comparison. Traversing the structure
    from the root node, a greater than or less than check will eliminate half of the data to be compared; assuming
    that it is a perfectly balanced tree.

    Each node in a binary search tree is similar to a doubly linked list in that they contain some data as well
    as two pointers to other nodes. The key difference from a doubly linked list is that the nodes relate to
    one another.

    A javascript implementation of such a node would look like the following:

    var node = {
    	data: 17,
    	left: null,
    	right: null
    }
    

    The first step in building a binary search tree implementation is to define a custom type with a single property
    that represents the root node.

    function BinarySearchTree() {
    	this.root = null;
    }
    

    To insert a value into the tree you must traverse the tree using the rules that are defined earlier in this document.
    The one special case is when no root node exists; denoting that the node to be inserted is the root node.

    BinarySearchTree.prototype = {
        insert: function(data){
            var node = {
                    data: data,
                    left: null,
                    right: null
                };
    
            var currentNode;
    
            if (this.root === null){
                this.root = node;
            } else {
                currentNode = this.root;
    
                while(true){
                    if (data < currentNode.data){
                        if (currentNode.left === null){
                            currentNode.left = node;
                            break;
                        } else {
                            currentNode = currentNode.left;
                        }
                    } else if (data > currentNode.data){
                        if (currentNode.right === null){
                            currentNode.right = node;
                            break;
                        } else {
                            currentNode = currentNode.right;
                        }
                    } else {
                        break;
                    }
                }
            }
        },
    };
    

    Removal of a node from a binary search tree is can be a complex operation because the
    tree must remain balanced. This means that all values on the left must be less than
    all of the values on the right. There are two special cases to consider when removing
    a node as well; existence of the node must be checked as well as determination if the
    node to be removed is the root node.

    When removing a node, the number of children for that node must be taken into consideration
    since the operations become slightly different depending on the number. Removing a node with
    two children is the most complex.

    BinarySearchTree.prototype = {
        remove: function(data){
    
            var found = false;
            var parentNode = null;
            var currentNode = this.root;
            var childCount;
            var replacementNode;
            var replacementParent;
                
            while(!found && currentNode){
                if (data < currentNode.data){
                    parentNode = currentNode;
                    currentNode = currentNode.left;
                } else if (value > current.value){
                    parentNode = currentNode;
                    currentNode = currentNode.right;
                } else {
                    found = true;
                }
            }         
    
            if (found){
                childCount = (current.left !== null ? 1 : 0) + 
                             (current.right !== null ? 1 : 0);
    
                if (currentNode === this.root){
                    switch(childCount){
                        case 0:
                            this.root = null;
                            break;
                        case 1:
                            this.root = (currentNode.right === null ? 
                                          currentNode.left : currentNode.right);
                            break;
                        case 2:
                            replacementNode = this.root.left;
    
                            while (replacementNode.right !== null){
                                replacementParent = replacementNode;
                                replacementNode = replacementNode.right;
                            }
    
                            if (replacementParent !== null){
                                replacementParent.right = replacementNode.left;
    
                                replacementNode.right = this.root.right;
                                replacementNode.left = this.root.left;
                            } else {
                                replacementNode.right = this.root.right;
                            }
    
                            this.root = replacementNode;
                    }        
                } else {
                    switch (childCount){
                        case 0:
                            if (currentNode.data < parentNode.data){
                                parent.left = null;
                            } else {
                                parentNode.right = null;
                            }
                            break;
                        case 1:
                            if (currentNode.data < parentNode.data){
                                parentNode.left = (currentNode.left === null ? 
                                               currentNode.right : currentNode.left);
                            } else {
                                parentNode.right = (currentNode.left === null ? 
                                                currentNode.right : currentNode.left);
                            }
                            break;
                        case 2:
                            replacementNode = currentNode.left;
                            replacementParent = currentNode;
    
                            while(replacementNode.right !== null){
                                replacementParent = replacementNode;
                                replacementNode = replacementNode.right;
                            }
    
                            replacementParent.right = replacementNode.left;
    
                            replacementNode.right = currentNode.right;
                            replacementNode.left = currentNode.left;
    
                            if (currentNode.data < parentNode.data){
                                parentNode.left = replacementNode;
                            } else {
                                parentNode.right = replacementNode;
                            } 
                    }
                }
            }
        },
    };
    

    A generic method to traverse the array is useful to have for cases where you may want
    to convert the values in the tree to an array or a string.

    BinarySearchTree.prototype = {
        traverse: function(evaluate){
            function iterate(node){
                if (node){
                    if (node.left !== null){
                        iterate(node.left);
                    }            
    
                    evaluate.call(this, node);
    
                    if (node.right !== null){
                        iterate(node.right);
                    }
                }
            }
            iterate(this.root);
        },
        
        toArray: function(){
            var result = [];
    
            this.traverse(function(node){
                result.push(node.data);
            });
    
            return result;
        },
    
        toString: function(){
            return this.toArray().toString();
        },    
    };
    

    Below are some simple usage examples for this implementation of a binary
    search tree in javascript.

    var bst = new BinarySearchTree();
    bst.add(17);
    bst.add(11);
    bst.add(43);
    bst.add(9);
    bst.add(65);
    bst.remove(43);
    
    document.writeln(bst.toString()); // prints 9 11 17 65
    
    
    Tags:
    August 13, 2012

    Introduction to data structures and algorithms in javascript: Doubly Linked Lists

    This is a continuation of a series introducing data structures in javascript. A linked list is a
    data structure consisting of a group of nodes that represent a sequence. Each element in a linked
    list has a data field and a field that points to the the next node in the linked list. A doubly
    linked list also includes a field that points to the previous node in the linked list.

    The first step in creating a doubly linked list in javascript is to define a custom type. A doubly
    linked list should be defined with a length property, a ‘head’ property which points to the first
    element in the list, and a ‘tail’ property which points to the last element in the list.

    function DoublyLinkedList() {
    	this.length = 0;
    	this.head = null;
    	this.tail = null;
    }
    

    Adding an item to the list is simply a matter of updating the ‘tail’ property with the new item and
    updating the previous ‘tail’ item to have a ‘next’ value of the new node. If the length of the list
    is zero, the ‘head’ and ‘tail’ properties are set to the node that is being added; making it the
    first item in the list.

    DoublyLinkedList.prototype = {
    	add: function(value) {
    		var node = {
    			value: value,
    			next: null,
    			previous: null,
    		}
    		
    		if (this.length == 0) {
    			this.head = node;
    			this.tail = node;
    		}
    		else {
    			this.tail.next = node;
    			node.previous = this.tail;
    			this.tail = node;
    		}
    		
    		this.length++;
    	},
    };
    

    To retrieve a value from the list, it requires that you traverse the list to find the node for a
    given index. If an index is provided that does not exist in the list, then a null value should be
    returned.

    DoublyLinkedList.prototype = {
    	getNode: function(index) {
    		if ( index > this.Length - 1 || index < 0 ) {
    			return null;
    		}
    		
    		var node = this.head;
    		var i = 0;
    		
    		while (i++ < index) {
    			node = node.next;
    		}
    		
    		return node;
    	},
    	
    	displayNode: function(index) {
    		var node = this.getNode(index);
    		if (node != null) {
    			document.writeln('value = ' + node.value + '<br />');
    			document.writeln('previous = ' + (node.previous != null ? node.previous.value : 'null') + '<br />');
    			document.writeln('next = ' + (node.next != null ? node.next.value : 'null') + '<br />' );
    			return;
    		}
    		
    		alert('invalid index!');
    	},
    };
    

    Note that displayNode is just a convenience function for the purpose of this demonstration. In any
    case, you should check that the previous or next node is not null before attempting to access the
    value.

    The final core operation of implementing a doubly linked list is providing the ability to remove an
    element. Removing an element from the list is a bit tricky because the previous node and next node
    will need to have their properties updated. Any remove operation should handle the case where the
    element to be removed is the first or last one. In both of these cases, you will need to update the
    ‘tail’ and ‘head’ property appropriately. Removing all other elements involves a similar lookup that
    is done in the getNode() function. The length should also be manually updated.

    DoublyLinkedList.prototype = {
    	remove: function(index) {
    		if ( index > this.Length - 1 || index < 0 ) {
    			return null;
    		}
    		
    		var node = this.head;
    		var i = 0;
    		
    		if (index == 0) {
    			this.head = node.next;
    			
    			// check if we removed the only one in the list
    			if (this.head == null) {
    				this.tail = null;
    			}
    			else {
    				this.head.previous = null;
    			}
    		}
    		else if (index == this.length - 1) {
    			node = this.tail;
    			this.tail = node.previous;
    			this.tail.next = null;
    		}
    		else {
    			while (i++ < index) {
    				node = node.next;
    			}
    			
    			node.previous.next = node.next;
    			node.next.previous = node.previous;
    		}
    		
    		this.length--;
    	},
    };
    

    For convenience, the following is the complete implementation with sample usage.

    function DoublyLinkedList() {
    	this.length = 0;
    	this.head = null;
    	this.tail = null;
    }
    
    DoublyLinkedList.prototype = {
    	add: function(value) {
    		var node = {
    			value: value,
    			next: null,
    			previous: null,
    		}
    		
    		if (this.length == 0) {
    			this.head = node;
    			this.tail = node;
    		}
    		else {
    			this.tail.next = node;
    			node.previous = this.tail;
    			this.tail = node;
    		}
    		
    		this.length++;
    	},
    	
    	getNode: function(index) {
    		if ( index > this.Length - 1 || index < 0 ) {
    			return null;
    		}
    		
    		var node = this.head;
    		var i = 0;
    		
    		while (i++ < index) {
    			node = node.next;
    		}
    		
    		return node;
    	},
    	
    	displayNode: function(index) {
    		var node = this.getNode(index);
    		if (node != null) {
    			document.writeln('value = ' + node.value + '<br />');
    			document.writeln('previous = ' + (node.previous != null ? node.previous.value : 'null') + '<br />');
    			document.writeln('next = ' + (node.next != null ? node.next.value : 'null') + '<br />' );
    			return;
    		}
    		
    		alert('invalid index!');
    	},
    	
    	remove: function(index) {
    		if ( index > this.Length - 1 || index < 0 ) {
    			return null;
    		}
    		
    		var node = this.head;
    		var i = 0;
    		
    		if (index == 0) {
    			this.head = node.next;
    			
    			// check if we removed the only one in the list
    			if (this.head == null) {
    				this.tail = null;
    			}
    			else {
    				this.head.previous = null;
    			}
    		}
    		else if (index == this.length - 1) {
    			node = this.tail;
    			this.tail = node.previous;
    			this.tail.next = null;
    		}
    		else {
    			while (i++ < index) {
    				node = node.next;
    			}
    			
    			node.previous.next = node.next;
    			node.next.previous = node.previous;
    		}
    		
    		this.length--;
    	},
    };
    
    var list = new DoublyLinkedList();
    list.add("zero");
    list.add("one");
    list.add("two");
    list.add("three");
    
    list.displayNode(2); // prints value = two, previous = one, next = 3
    list.remove(2);
    list.displayNode(2); // prints value = three, previous = one, next = null
    
    Tags:
    August 12, 2012

    Introduction to data structures and algorithms in javascript: Stacks and Queues

    This is a continuation of a series introducing data structures in javascript. In the last couple of
    posts, the javascript Array was introduced and it is recommended to have an understanding of the
    javascript Array before reading this article.

    Stack

    A stack is a linear data structure and abstract data type in which operations are performed via the
    last in, first out (LIFO) methodology. Similar to other programming languages there are two main
    methods in javascript used to populate and retrieve data in the stack. These methods are ‘push’
    and ‘pop’. The ‘push’ method is used to add an element to the top of stack and the ‘pop’ method is
    used to remove the top element from the stack.

    In javascript, the Array object is used for a stack implementation. Javascript has a push() and a
    pop() method for the Array object which makes the implementation rather simple.

    	var stack = new Array();
    	stack.push("one");
    	stack.push("two");
    	stack.push("three");
    
    	document.writeln(stack.pop() + " - stack length:" + stack.length); // prints "three - stack length: 2"
    	document.writeln(stack.pop() + " - stack length:" + stack.length); // prints "two - stack length: 1" 
    	document.writeln(stack.pop() + " - stack length:" + stack.length); // prints "one - stack length: 0" 
    

    One key point to understand is that when pushing an element to the stack it is adding it to the end
    of the Array. This may be counter intuitive since it is commonly referred to as the “top of the stack”.

    Another point to understand is that when you use the pop method it removes the last element from the
    Array as indicated in the code sample when it prints the Array length.

    Queue

    A queue is also a linear data structure and abstract data type with one key difference from a stack
    being that it uses a first in, first out (FIFO) methodology. Another name for a queue is a buffer.
    Typical usage of a queue would be for instances where you have more data to manipulate than you can
    handle in a single period of time.

    In javascript, the Array object is also used for a queue implementation. The unshift() method is
    used to add elements to the beginning of an Array; ensuring that when you use pop() you get the first
    element that was added to the Array.

    	var queue = [];
    	queue.unshift("one");
    	queue.unshift("two");
    	queue.unshift("three");
    
    	document.writeln(queue.pop() + " - queue length:" + queue.length); // prints "one - queue length: 2"
    	document.writeln(queue.pop() + " - queue length:" + queue.length); // prints "two - queue length: 1" 
    	document.writeln(queue.pop() + " - queue length:" + queue.length); // prints "three - queue length: 0" 
    
    Tags:
    August 6, 2012

    Introduction to data structures and algorithms in javascript: Arrays pt. 2

    This is a continuation of a series on data structures in javascript. If you have been following
    along, the last post introduced the javascript Array. This week we will take a closer look at some of the
    methods available in javascript to manipulate an Array.

    Array.concat

    Array.concat joins two or more Arrays to create a new Array. Since it creates a new Array, it’s
    important to understand that the original Array’s will remain unchanged.

    var someArray = [ 1, 2, 3 ];
    var anotherArray = [ 4, 5, 6 ];
    
    var combinedArray = someArray.concat(anotherArray);
    document.writeln(combinedArray); // prints 1, 2, 3, 4, 5, 6
    

    Array.every

    Array.every is a method that accepts a function as an argument. Each element in the Array is passed
    to the function and evaluates if a condition is true or false. If all elements return true for the
    Array, then the every method will return true. If at least one element returns false, then the
    every method will return false.

    var someArray = [ 1, 2, 3 ];
    var anotherArray = [ 4, 5, 6 ];
    var evaluateNum = 6;
    
    var isLessThan = function(value) {
    	return value < evaluateNum;
    }
    
    document.writeln(someArray.every(isLessThan)); // prints 'true'
    document.writeln(anotherArray.every(isLessThan)); // prints 'false'
    

    Array.filter

    Array.filter will create a new Array of elements that evaluate to true in the given function. This
    method passes the current value, the index, and a pointer to the array to the function.

    var someArray = [ 1, 2, 3, 4, 5, 6 ];
    var evaluateNum = 4;
    
    var isLessThan = function(value) {
    	return value < evaluateNum;
    }
    
    document.writeln(someArray.filter(isLessThan)); // prints 1, 2, 3
    

    Array.forEach

    Array.forEach passes each element of the Array to a given function. This is all this method does.
    There is no return value. It’s simply an alternate to the common for loop and may be useful for
    reusing common logic while looping through Array’s.

    var someArray = [ 1, 2, 3 ];
    
    var printArray = function(value, index) {
    	document.writeln(index +' - '+ value);
    }
    
    someArray.forEach(printArray); // prints 0 - 1, 1 - 2, 2 - 3
    

    Array.join

    Array.join will output an Array as a string with a given delimiter. It is useful for sending a
    string to the server that can be parsed using the delimiter.

    var someArray = [ 1, 2, 3 ];
    var delimited = someArray.join('-');
    
    document.writeln(delimited); // prints 1-2-3
    

    Array.indexOf

    Array.indexOf will search the Array until it finds a match for a give search criteria. Once the
    match is found, it will return the index of the matching element. It is important to note that it
    will return the first match only. It will return -1 if no matches are found.

    var someArray = [ 1, 2, 3, 3, 4, 5 ];
    
    document.writeln(someArray.indexOf(3)); // prints 2
    document.writeln(someArray.indexOf(9)); // prints -1
    

    Array.lastIndexOf

    Array.lastIndexOf provides the same functionality as Array.indexOf, but searches for a match in
    reverse order.

    var someArray = [ 1, 2, 3, 3, 4, 5 ];
    
    document.writeln(someArray.lastIndexOf(3)); // prints 3
    document.writeln(someArray.lastIndexOf(9)); // prints -1
    

    Array.map

    Array.map will return a new array containing values in the Array that are determined by a given
    function.

    var someArray = [ 1, 2, 3, 4, 5, 6 ];
    var evaluateNum = 4;
    
    var isLessThan = function(value) {
    	if  (value < evaluateNum) {
    		return 1;
    	}
    	return 0;
    }
    
    var newArray = someArray.map(isLessThan);
    document.writeln(newArray); // prints 1, 1, 1, 0, 0, 0
    

    Array.reverse

    Array.reverse intuitively reverses the order of the Array.

    var someArray = [ 1, 2, 3, 4, 5, 6 ];
    someArray.reverse();
    
    document.writeln(someArray); // prints 6, 5, 4, 3, 2, 1
    

    These are the most commonly used useful Array methods. In the next post, we will introduce some of
    the more advanced methods including methods used for creating stacks and queues.

    Tags: