Page 2 of 2 FirstFirst 12
Results 11 to 16 of 16

Thread: Favorite books?

  1. #11
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Okay I see your point. So you need a JavaScript book that isn't too easy, but sort of easy. Just enough to get the hang of it.
    Anyways...
    what for-loop is, how it works, and give an example of how it's used
    Easy.
    A for loop in JS is exactly the same as C++ (just isn't as strict). There's only one type of variable in JS, no int, double, etc. So. Here's a basic for-loop

    Code:
    for (var i = 0;i < 25;i++) {
    	document.write(i)
    	//display the value of "i"
    	}
    For-loops can be useful in verifying information, running through a sequence of arrays etc.

    An array is a variable that stores data in sequence:
    Code:
    var myArray = ["value 0","value 1","value 2","value 3","this is another value"]
    So you could say...
    Code:
    document.write(myArray[1])
    Which would display "value 1"
    Not that hard when you look at it from a glance.
    So if you can store data using a decimal value, here's where your for-loop comes in.
    Let's take a look at this code:
    Code:
    <html>
    <head>
    <script type="text/javascript">
    function results(contestant) {
    var entries = ["jonathan","damien","courtney","kyle"]
    for (var i = 0;i < entries.length;i++) {
    	if (contestant.value == entries[i]) {
    		alert(entries[i]);
    		}
    	}
    }
    </script>
    </head>
    <body>
    Type in the winners name:
    <br><input type="text" id="winner">
    <br><input type="button" value="Go" onclick="results(winner)">
    </body>
    </html>
    The "entries" variable are all the possibilities. The for-loop runs numbers from 0 to the array "entries" length (entries.length is how many values there are in entries). If a winner is found in the input (the argument "contestant") it will alert saying the winners' name. Not too complex, just a little experiment with the for-loop.
    - Mike

  2. #12
    Join Date
    Oct 2005
    Posts
    65
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    lol, i appreciate explaining the for-loop to me, but I already knew that =)

    i just used loops as an example, since we were already talking about it in the post before.

    But I wasn't sure exactly how to declare arrays, so that's a plus!

    Also, I know that there is only one type of variable in javascript, but what happens if you try to add 1 variable with text to a variable with numbers? Will it result in StringNumber? or will it give you an error? Will it just flat out not work?

    Hrm... this is getting a bit off topic...

  3. #13
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    When you add a number variable to a string, the browser defines the "+" operater as an assignment operator, not addition. So it won't try to add the string and the number, it'll just add the two on top of each other

    Ex.

    Code:
    var a = 2
    var b = "string"
    document.write(b+a)
    Will turn out "string2" just like that
    - Mike

  4. #14
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    Check out this book. I haven't read it yet(plan to get it soon), but if it is anything like the website is should be great. I have also read good things about this book.

  5. #15
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    the browser defines the "+" operater as an assignment operator
    Concatenation, not assignment.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #16
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Concatenation, not assignment.
    Ah yes, my mistake.
    - Mike

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •