Jumat, 25 Oktober 2013

Xathrya Sabertooth

Xathrya Sabertooth


JavaScript: String Objects

Posted: 24 Oct 2013 06:38 AM PDT

This article is a supplement for Compact Tutorial on JavaScript Basic.

We can create a string primitives by giving them some characters to hold.

var myPrimitiveString = "Xathrya Sabertooth";

A String object does things slight differently, not only allowing us to store characters, but also providing a way to manipulate and change those characters.

Creating String Object

Declaring a new variable and assign it a new string primitive to initialize it. Now we try using typeof() to make sure data in the variable myPrimitiveString:

<HTML>  <BODY>  <SCRIPT type="text/javascript">      var myPrimitiveString = "Xathrya Sabertooth";      document.write (typeof(myPrimitiveString));  </SCRIPT>  </BODY>  </HTML>

We can still use the String object’s methods on it, though. JavaScript will simply convert the string primitive to a temporary string object, use the method on it, and then change the data type back to string. Now let’s try out using the length property of the String object.

<HTML>  <BODY>  <SCRIPT type="text/javascript">      var myPrimitiveString = "Xathrya Sabertooth";      document.write ( typeof( myPrimitiveString ) );      document.write ( "<br>" );      document.write ( myPrimitiveString.length );      document.write ( "<br>" );      document.write ( typeof( myPrimitiveString ) );  </SCRIPT>  </BODY>  </HTML>

Which should give this result:

string  18  string

So, myPrimitiveString is still holding a string primitive after the temporary conversion. You can also create String objects explicitly, using the new keyword together with the String() constructor.

<HTML>  <BODY>  <SCRIPT type="text/javascript">      var myObjectString = new String("Xathrya Sabertooth");      document.write ( typeof( myObjectString) );      document.write ( "<br>" );      document.write ( myObjectString.length );      document.write ( "<br>" );      document.write ( typeof( myObjectString) );  </SCRIPT>  </BODY>  </HTML>

Which should give this result:

object  18  object

The only difference between this script and the previous one is the myObjectString is a new object, created and supplied with some characters.

var myObjectString = new String("Xathrya Sabertooth");

The result of checking the length property is the same whether we create the String object implicitly or explicitly. The only real difference is that creating string object explicitly is marginally more efficient if we're going to be using the same String object again and again. Explicitly creating String objects also helps prevent the JavaScript interpreter getting confused between numbers and strings, as it can do.

String Object’s Methods

The String object has a lot of methods, so we will limit to two of them: indexOf() and substring() methods. The more complete list of String object’s properties and methods can be found at the handout section.

String is made of characters (it is sequence of characters). Each of these characters is given an index, zero-based index. So the first character’s position has the index 0, the second is 1, and so on. The method indexOf() finds and returns the position in the index at which a substring begins (and the lastIndexOf() method returns the position of last occurrence of the substring).

We can use this method for example, checking the position (and existence) of symbol @ in our entry when we ask user to input email address. No guarantee that the email address is valid, but at least it would go some way to that direction.

We will use prompt() function to obtain the user’s e-mail address and then check the input for the @ symbol, returning the index of the symbol using indexOf().

<html>  <body>  <script type="text/javascript">      var userEmail= prompt("Please enter your emailaddress ", "" );      document.write( userEmail.indexOf( "@" ) );  </script>  </body>  </html>

If the @ is not found, -1 is written to the page. As long as the character is in string, the indexOf() will return the index, something greater than -1.

The substring() method carves one string from another string. It takes index of start and end position of the substring as parameters. We can return everything from the first index to the end of the string by leaving off the second argument.

So, to extract all the character from the third character (at index 2) to the sixth character (index 5), we can write as:

<html>  <body>  <script type="text/javascript">      var characterName = "I am Xathrya Sabertooth";      var lastNameIndex = characterName.indexOf( "Xathrya " ) + 8;      var lastName = characterName.substring( lastNameIndex );      document.write( lastName );  </script>  </body>  </html>

We are extracting Sabertooth from the stirng in the variable characterName. We first find the start of the first name and add it with 8 to find the index of the first name (because “Xathrya ” is 8 characters long). We use indexOf here. The result is stored on lastNameIndex. Using that value, we extract the substring of lastname, form lastName Index to unspecified final index. The rest of the characters in the string will be returned.

Handouts

  1. JavaScript String Properties and Methods List (PDF)

Introduction to Network Topology

Posted: 24 Oct 2013 04:49 AM PDT

Network topology is a term which refer to the arrangement of various elements (links, nodes, etc.) of a computer network. Every node are arrange in structure which allows nodes to be interconnected. The structure may be depicted physically or logically. Physical topology refers to the placement of the network’s various components, including device location and cable installation. Logical topology refers to arrangement on how data flows within a network, regardless of its physical design. Distance between nodes, physical interconnections, transmission rates, and/or signal types may differ between two networks, yet their topologies may be identical.

After years of study on networking, one can mention some popular topology, based on it’s physical layout. They are:

  1. Bus topology
  2. Ring topology
  3. Star topology
  4. Tree topology
  5. Mesh topology

Aside of them, there are also a number of combination from above, which is also known as hybrid structure.

NetworkTopologies

Bus Topology

BusNetwork      bus-topology

In bus topology, all nodes are connected to a single cable, known as bus. A signal from the source travels in both directions to all machines connected on the bus cable until it finds the intended recipient. In other node, if the machine address does not match the intended address for the data, the machine ignores the data.Alternatively, if the data matches the machine address, the data is accepted.

The bus topology only needs one wire. A terminator is used in both side of bus / cable, which helps to send data inside the bus. Since the bus topology consists of only one wire, it is rather inexpensive to implement when compared to other topologies. However, the cost of managing the network is rather high. It is also considered single point of failure, which means when the bus is unavailable, the network would go down.

Ring Topology

RingNetwork      ring_topology

Like implied by the name, ring topology is set up in a circular form. Using this method, data travels around the ring in one direction an each device on the ring acts as a repeater to keep the signal strong as it travels. Each node can act as as receiver and transmitter, they become receiver for the incoming signal, and a transmitter to send the data on to the next node in the ring.

Every node is a critical link. When one node is down, the network would go down also.

Star Topology

StarNetwork     star-topology

A network created using star topology use a single node act as central. A hub or switch is used for this function. They are then connected point-to-point with other node the member of the network. If we see this as server-client architecture, we can see the switch is the server and the peripherals or other nodes are the clients.

All traffic that traverses the network passes through the central switch. Switch then multiplex the data and send / relay the data to the destination, based on their address.

The network does not necessarily have to resemble a star to be classified as star network, but all the nodes on the network must be connected to one central point.

The star topology is considered the easiest topology. The primary advantage of this topology is the modularity. Adding and removing nodes is really simple. However, the switch (the center node) itself is very critical, represents a single point of failure.

Tree Topology

TreeNetwork

This is rather complex topology, based on arranging nodes in hierarchical system. Like any tree concept, in tree network exist a single, ‘root’ node. This node connected either a single (or multiple) node(s) in the level below by point-to-point link. These lower level nodes are also connected to a single or multiple nodes in the next level doen.

Tree networks are constrained to any number of levels. But as tree networks are a variant of the bus network topology, they are prone to crippling network failures when the higher level of nodes fail or suffer damage. Each nodes has a specific, fixed number of nodes connected to it at the next lower level in the hierarchy. This number is referred to ‘branching factor’ of the tree.

  1. A network that is based upon the physical hierarchical topology must have at least three levels in the hierarchy of the tree, since a network with a central ‘root’ node and only one hierarchical level below it would exhibit the physical topology of a star.
  2. A network that is based upon the physical hierarchical topology and with a branching factor of 1 would be classified as a physical linear topology.
  3. The branching factor, f, is independent of the total number of nodes in the network and, therefore, if the nodes in the network require ports for connection to other nodes the total number of ports per node may be kept low even though the total number of nodes is large – this makes the effect of the cost of adding ports to each node totally dependent upon the branching factor and may therefore be kept as low as required without any effect upon the total number of nodes that are possible.
  4. The total number of point-to-point links in a network that is based upon the physical hierarchical topology will be one less than the total number of nodes in the network.
  5. If the nodes in a network that is based upon the physical hierarchical topology are required to perform any processing upon the data that is transmitted between nodes in the network, the nodes that are at higher levels in the hierarchy will be required to perform more processing operations on behalf of other nodes than the nodes that are lower in the hierarchy. Such a type of network topology is very useful and highly recommended.

Mesh Topology

In Mesh topology, each node must not only capture and disseminate its own data, but also serve as a replay for other nodes. That is, it must collaborate to propagate the data in the network.

The self-healing capability of mesh enables a routing based network to operate when one node breaks down or a connection goes bad. As a result, the network is typically quite reliable, as there is often more than one path between a source and a destination in the network.

Mesh network can be divided into two categories: partially connected mesh and fully connected mesh.

Partially connected

In this type of network topology, some of the nodes are connected to more than one other node in the network with a point-to-point link.

NetworkTopology-Mesh

Fully connected

A fully connected mesh is a mesh with complete arc from one node to all remaining nodes. This means each of the nodes is connected to each other. No switching nor broadcasting need as every node know all peer. However, the number of connections grows quadratically with the number of nodes.

connections = node * (node - 1) / 2

This topology is impractical for large-scale network.

NetworkTopology-FullyConnected

Tidak ada komentar:

Posting Komentar