Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> The Basics Of Javascript 3, o.O arrays are involved
DanWilliamson
post May 31 2006, 05:03 AM
Post #1


Regular Dot
Group Icon
Group: *Premier*
Posts: 56
Joined: 20-March 06
From: Manchester UK
Member No.: 10,400





This is the third tutorial in the javascript basics written by me and this is where we get quite advanced and will use Functions, Variables, alerts, document writes etc without explaining them so if you don't understand them please read the other two tutorials.

Arrays are used in my opinion so that you don't have hundreds of variables and just a few nice arrays so it's used to hold variables. Here is a quick example of code using variables and then code using arrays.

CODE
<script type="text/javascript">
<!--

    var monday = Monday;
    var tuesday = Tuesday;
    var wednesday = Wednesday;
    var thursday = Thursday;
    var friday = Friday;
    var saturday = Saturday;
    var sunday = Sunday;

  document.write("Todsay is monday");

//-->
</script>


So there we have the variables and the document write, sorry if there are any mistakes as I jotted that up quickly. Here is the array version which is much better.

CODE
<script type="text/javascript">
<!--

    var days = new Array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");

  document.write("The day is " + days[5] + "!");

//-->
</script>


This will output 'The day is Saturday!' and can be used differently or whatever but the basic rundow is that you define a variable then the equal operator and type new Array(""); I used days of the week so we didn't go too advanced but heres another way to create arrays and this is the way that I first ever learned.

CODE
<script type="text/javascript">
<!--

    var days = new Array(3);
        days[0] = "Monday";
    days[1] = "Tuesday";
    days[2] = "Wednesday";

//-->
</script>


Although the code is longer it is more easily set out for the beginner to arrays, also you must note that new Array(3); was used as you have [0][1][2] which are the (3) if that made any sense to yeah, anyway I haven't added all the days of the week as that would be really long and I don't have the typing energy. We haven't used arrays for much yet but here is something we could do with the if/else condition but I will save that for another day.

Hopefully I will write a few more javascript tutorials for 13Dots as it's been great victory.gif


IPB
 
+Quote Post  Go to the top of the page
RedDragon
post May 31 2006, 07:08 AM
Post #2


insert coin
Group Icon
Group: Main Team
Posts: 5,344
Joined: 24-May 04
From: Maastricht
Member No.: 35





Somebody is on fire!

Thanks for Sharing Dan smile.gif


IPB
 
+Quote Post  Go to the top of the page

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

RSS Lo-Fi Version Time is now: 6th January 2009 - 01:04 AM
Watch American Dad | Bankruptcy | Modded Xbox | Mobile Phone | Salvage cars