Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6

Thread: File List Array

  1. #1
    New Coder
    Join Date
    Jun 2014
    Location
    Hastings, UK
    Posts
    27
    Thanks
    3
    Thanked 0 Times in 0 Posts

    File List Array

    Please how can I create an array list of say all the MP3 files in the same folder as the HTM Page, such as below. Except I want to use

    <bgsound src="short.mp3" loop="1">

    that only works with IE to play the MP3 files, instead of

    document.write(mycars[x] + "<br />");.

    Below does not work anyway unless I have missed some mistake with my eyesight.

    Code:
    <html>
    <body>
    <script type="text/javascript">
    var x;
    var mycars = new Array();
    mycars[0] = "saab";
    mycars[1] = "volvo";
    mycars[2] = "BMW";
    
    for (x = mycars)
    {
    document.write(mycars[x] + "<br />");
    }
    </script>
    </body>
    </html>

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,683
    Thanks
    25
    Thanked 656 Times in 655 Posts
    Your FOR is written wrong
    Code:
    <html>
    <body>
    <script type="text/javascript">
    //var x;   NOT NEEDED IT'S DEFINED IN THE LOOP
    var mycars = new Array();
    mycars[0] = "saab";
    mycars[1] = "volvo";
    mycars[2] = "BMW";
    
    for (x = 0; x<mycars.length; x++){
    	document.write(mycars[x] + "<br />");
    }
    </script>
    </body>
    </html>
    But it looks like you didn't read far enough on the w3schools page. Near the bottom is this:
    Avoid new Array()

    There is no need to use the JavaScript's built-in array constructor new Array().

    Use [] instead.

    These two different statements both create a new empty array named points:
    var points = new Array(); // Bad
    var points = []; // Good

    These two different statements both create a new array containing 6 numbers:
    var points = new Array(40, 100, 1, 5, 25, 10) // Bad
    var points = [40, 100, 1, 5, 25, 10]; // Good


    The new keyword complicates your code and produces nasty side effects:
    var points = new Array(40, 100); // Creates an array with two elements (40 and 100)

    What if I remove one of the elements?
    var points = new Array(40); // Creates an array with 40 undefined elements !!!!!
    So the final code S/B:
    Code:
    <html>
    <body>
    <script type="text/javascript">
    var mycars = ["saab", "volvo", "BMW"];
    
    for (x = 0; x<mycars.length; x++){
    	document.write(mycars[x] + "<br />");
    }
    </script>
    </body>
    </html>
    I don't think this will solve your problem. Please explain more fully what is going on.
    Last edited by sunfighter; 07-28-2014 at 03:33 PM.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #3
    New Coder
    Join Date
    Jun 2014
    Location
    Hastings, UK
    Posts
    27
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks. If you care to look among the Freeware on my site, I have programs that create a list of say all the MP3 files in any folder then plays them all one at a time unless halted, by selecting each one at random. If JS can do anything done by Basic I would like to be able to also create a HTM file version.

    So I will also need to know the JS code for RND used in Basic please.

  • #4
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,683
    Thanks
    25
    Thanked 656 Times in 655 Posts
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #5
    New Coder
    Join Date
    Aug 2014
    Posts
    27
    Thanks
    0
    Thanked 1 Time in 1 Post
    JavaScript arrays are utilized to store multiple values in a single variable. With javascript all arrays are flexible. You can easily do something like the following:
    var myArray = new Array();
    myArray.push("string 1");
    myArray.push("string 2");

  • #6
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,683
    Thanks
    25
    Thanked 656 Times in 655 Posts
    @janisspiker12,
    You do realize this is 20 days old and probably done with that long ago? Reopening it does send me an email and I do have to come and look at it and I am now wondering "What are you trying to say"?.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."


  •  

    Posting Permissions

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