Web Analytics Made Easy -
StatCounter Returning Arrays/Variables? - CodingForum

Announcement

Collapse
No announcement yet.

Returning Arrays/Variables?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Returning Arrays/Variables?

    Whats up everybody, I'm a bit new to javascript and I'm having some problems returning values of my functions, I need to finish my wife's slideshow so she can send it to her parents and I've been going over this for quite a while and I have no clue why I'm not getting the results I should be.
    I think it might have something to do with my return pictureArray but thats the only thing I can think of
    here is the code
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
    <html lang="en">
    <head>
        
        <title>Vacation Photos</title>
    </head>
    <body>
        
            <img id="mystyle" src="original.jpg" alt="NA" />
        <hr />
        <form>   
        <p>
            folder name: <input type="text" id="foldername" value="vacpics/" size="20" /> <br />
        </p>
        <p>
            simname: <input type="text" id="simname" value="vacation" size="10" /> <br />
        </p>
        <p>
            Starting Photo <input type="text" id="Start" value="1" size="13"/>
            
            Ending Photo  <input type="text" id="End" value="1" size="13"/>  
        </p>
        
        <p>
            <input type="button" id="SlideShow" value=" Regular Slide Show" />
            <input type="button" id="RandomShow" value="Random Slide Show" />
            <input type="button" id="Prev" value="Prev"  />
            <input type="button" id="NextSlide" value="Next " />
        </p>
        </form>
        
        
        
    <script type="text/javascript">
    var photodir= endValue-startValue+1;
    var pictureArrayl= new array (photodir);
    
    main();
    function main() {
        document.getElementById("SlideShow");
        onClick = getPictureNames(); 
        
    
    function getPictureNames() {
    	var folderNameTextField = document.getElementById("foldername");
    	var text = folderNameTextField.value;
    	var folderNameValue =(text);
    	
    	var simNameTextField=document.getElementById("simname");
    	var text2 = simNameTextField.value;
    	var simNameValue =(textProvided2);
    	
    	var start = document.getElementById("Start");
    	var text3 = start.value;
    	var startValue =(text3);
    	
    	var end = document.getElementById("End");
    	var text4 = end.value;
    	var endValue =(text4);
    	
    	var totalValue= folderNameValue+simNameValue+startValue;
    	
    	for (photodir=startValue;photodir<endValue;photodir++)
    	{
    	    pictureArray[0] = "original.jpg";
    	    pictureArrayl[1] = totalValue+ ".jpg";
    	    pictureArray[2] = totalValue+ ".jpg";
    	   
    	    
    	    return pictureArray;
    	}
    	
    	
    	    
        return totalValue;  
    }
    }
    thanks for the help, I really do appreciate it.

  • #2
    You return pictureArray is *INSIDE* the loop!

    So you will only do that loop code the FIRST TIME and then immdiately return!

    Also... You hvae a pretty useless return totalValue after the loop. That will *NEVER* get reached, because the return inside the loop will have already happened.

    Lots of other stuff there that doesn't make sense.

    YOu are defining the getPictureNames( ) function *inside* the main( ) function. Don't do it.

    You are doing
    Code:
    onClick = getPictureNames();
    I don't know what you think that is doing, but it's actually doing nothing at all.

    You are assigning the array you get from getPictureNames() to a *VARIABLE* that just happens to be named onClick. Since you never do anything with that variable, nothing happens.

    I really think you need to toss the code and start over. Sorry.

    Maybe if you told us what it was *supposed* to do we could help?
    Be yourself. No one else is as qualified.

    Comment


    • #3
      I agree with Old Pedant. There is no point in trying to re-invent the wheel, but making it oval this time. There are many excellent slideshow scripts out there.

      For example:-

      Code:
      <HTML>
      <HEAD>
      </HEAD>
      <BODY>
      
      <center>
      <td align="center" width="100%"><img src="One.gif" style="width:80px;height:80px" name="photoslider"></td>
      </tr>
      
      <tr>
      <td width="100%">
      <div align="center">
      
      <script type= "text/javascript">
      
      var photos=new Array();
      var which=0;
      
      photos[0]="One.gif"
      photos[1]="Two.gif"
      photos[2]="Three.gif"
      photos[3]="Four.gif"
      photos[4]="Five.gif"
      photos[5]="Six.gif"
      
      function backward(){
      if (which>0){
      window.status='';
      which-- ;
      document.photoslider.src=photos[which];
      }
      }
      
      function forward(){
      if (which<photos.length-1){
      which++ ;
      document.photoslider.src=photos[which];
      }
      else window.status='End of gallery';
      }
      
      </script>
      
      <br>
      <input type="button" value="&lt;&lt; Back" name="B2" onClick="backward()"> &nbsp &nbsp <input type="button" value="Next &gt;&gt;" name="B1" onClick="forward()"><br><br>
      
      <a href="#" onClick="which=1;backward();return false"><font face="Arial, Helvetica" color="#0000FF"><small><b>Go back to the start</b></small></a></p>
            
      <a href="#" onClick="which=photos.length-2; forward();return false"><font face="Arial, Helvetica" color="#0000FF"><small><b>Go to the last image</b></small></a></p>
      
      </center></div>
      
      
      </BODY>
      </HTML>

      All the code given in this post has been tested and is intended to address the question asked.
      Unless stated otherwise it is not just a demonstration.

      Comment


      • #4
        A lot of errors, First, HTML. Why do you end the non-empty tags (input) as in XHTML (like <input /> or <br />) as long as you use a HTML Doctype? Nor the Doctype is correct. More about Doctype:
        http://www.w3schools.com/tags/tag_DOCTYPE.asp

        Javascript:

        In javascript a function has to be called by an event (onclick, onload, onmouseover....) following the user's action (click on something, loading the element, rollover the mouse...). Where is your event?

        Another thing:
        Code:
        var photodir= [COLOR="Red"]endValue[/COLOR]-[COLOR="Red"]startValue[/COLOR]+1;
        There are no global variables called endValue and startValue. What you have here:
        Code:
        function getPictureNames() {
        [COLOR="Blue"]var[/COLOR] startValue =(text3);
        ...
        [COLOR="Blue"]var[/COLOR] endValue =(text4);
        }
        are local variables (they live only inside the function)
        More about javascript variables:
        http://www.webdevelopersnotes.com/tu...avascript.php3

        And:
        Code:
        var folderNameValue =[COLOR="Red"]([/COLOR]text[COLOR="Red"])[/COLOR];
        What is the use of the parenthesis? The value will not be valid unless the parenthesis nest a number (and even so they are useless unless you nest a math operation, which is not the case), but your value there is a string. You don't need the parenthesis.

        Furthermore:
        Code:
        function main() {
        [COLOR="Red"]    document.getElementById("SlideShow");
            onClick = getPictureNames(); 
        [/COLOR]    
        
        function getPictureNames() {[COLOR="Red"]//? Why nested[/COLOR]
        }
        
        }
        What those red lines suppose to do? They are not correct, from the syntax point of view. And why have you nest a function inside another function?
        KOR
        Offshore programming
        -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

        Comment

        Working...
        X