Jump to content

Why Isn't This Logic Understanding Me...


daring_du
 Share

Recommended Posts

Hi, I'm taking an informal javascript class. My teacher gave me this fading pictures script for practice, but unfortunately he doesn't seem to understand it all that well himself.I have two issues. I've put my questions in comment tags to make it easier to see which piece of code I am referring to. There are an issue at the beginning and the end which I'd like help with. I posted this on a different forum and someone responded that j=0 should never be true, but that's all the explanation I got, which didn't really help much since I can already see that if j=0 is true then the script doesn't work. The main issue for me here is in understanding the logic of how this script is set up. I've got several more questions about it, but these two are concerning me the most at this point. Thanks for anyone who can help.

<script type="text/javascript">var crossFadeDuration=3//this variable is redundant because I can just input the number 3//into the appropriate place further down the page, unless//the author was trying to make a distinction between the two//kinds of duration regarding blendTrans.  Why is it listed twice?var Pic = new Array();Pic[0]="blah1.jpg";Pic[1]="blah2.jpg";Pic[2]="blah3.jpg";Pic[3]="blah4.jpg";Pic[4]="blah5.jpg;var j = 0;var preLoad = new Array();      for(i=0;i < pic.length;i++)   {   preLoad[i]= new Image();   preLoad[i].src = Pic[i]   }function runSlideShow(){   if(document.all)   {   document.images.SlideShow.style.filter="blendTrans(duration=2)";   document.images.SlideShow.style.filter="blendTrans(duration=crossFadeDuration)";   document.images.SlideShow.filters.blendTrans.Apply();   }   document.images.SlideShow.src=preLoad[j].src;   if(document.all)   {   document.images.SlideShow.filters.blendTrans.Play();   }   j = j+1;      if(j>(Pic.length-1)) j=0;//this is the real subject of my confusion.//I could also write this part as	if(j < (pic.length-5)) j=0	and the script will still work.//I can see that it is good for j=0 to be false.  When j=0 is true, the script does not work.//I can see that pic.length is the number of pics in the array, but why is it//necessary for j=0 to be false?  Why is this stupid j=0 important!setTimeout("runSlideShow()",5000);}</script>

Link to comment
Share on other sites

First of all, the filter property is exclusive to Internet Explorer, so this script isn't going to work anywhere else. It looks like a very old script as well. document.all and document.images are only seen in really old scripts these days and it's usually a sign that the script isn't really good.The logic here is simple:

if(j>(Pic.length-1)) j=0;

if j is greater than (Pic.length-1), then assign 0 to jIt can be rewritten as

if(j>(Pic.length-1)) {  j=0;}

The reason they do this is because when j is outside of the range of the array, it needs to be brought back in.About this:

var crossFadeDuration=3

Often, we use certain numbers in several parts of a script, if we want to change the script, rather than changing the number in every place that it's used, we just need to change one variable.

Link to comment
Share on other sites

hi Ingolme,Thanks for the fast response and thanks for the update about the apparently outdated script! I've been trying quite hard to find some kind of complete reference for keywords, reserved words and such things, but even after googling it's still quite confusing. Perhaps that's a problem with there being TOO MUCH information available. All the really good stuff is buried under junk. If you've got any links to some kind of reliable glossary or dictionary specifically for javascript terms that would be great.Anyway, about the crossFadeDuration question, your answer sounds reasonable. However, I was unclear as to why the author listed blendTrans twice and decided to name only 1 of them as a specific variable. Are both blendTrans interpreted by the browser in the same way and if so, what's the point of listing it twice? (unless listing it twice creates some kind of double effect. I tried playing around with the numbers to watch the effect as the script was working, but I found it difficult to notice the difference).You said...

The logic here is simple:CODEif(j>(Pic.length-1)) j=0;if j is greater than (Pic.length-1), then assign 0 to jIt can be rewritten asCODEif(j>(Pic.length-1)) {  j=0;}

The only difference I can see between the two examples is that the second one has curly braces around the "j=0" line of code. I'm not sure what difference that is supposed to make as the code still works without the curly braces.I'm not understanding why j needs to have a value of 0. What is the connection of j to Pic.length that it MUST =0 for every other piece of code to execute properly?you said...

The reason they do this is because when j is outside of the range of the array, it needs to be brought back in.
I'm not sure what it means that j is outside the range of the array or what was done to bring it back. j is mentioned 3 times, first just after the Pic=new Array and it's given a value of 0, second in document.images.SlideShow.src=preLoad[j].src; and third in the already discussed issue concerning the if statement at the bottom, where j is given a NEW value of 1.Perhaps there is some cascading connection as this j moves through the script that I'm just not getting. Is that even close? What is j!!! :)
Don't worry, it doesn't matter if the logic doesn't understand you, as long as you understand the logic.
ha, logic and I have a long history of discontent; it never gets tired of making me look stupid and that hurts my feelings. :)
Link to comment
Share on other sites

This is an array:

var X = new Array();X[0] = "A";X[1] = "B";X[2] = "C";

To access an array, we use its index:

// The following displays "B"alert(X[1]);

You can use variables to access array elements:

var j = 0;alert(X[j]); // This displays "A"

If j is greater than the highest index, then an error will occur:

var j = 5;alert(X[j]); // This won't do anything because the index "5" doesn't exist in this array

So we always make sure that j is less than the highest index of the array. We use the "length" propery, which tells us how many elements are in the array

var X = new Array();X[0] = "A";X[1] = "B";X[2] = "C";var j = 5;// X.length is 3// The highest index is one less than the length, which is 2if(j > X.length-1) j = 0;// Since 5 was not withing the elements of the array, j was set to 0 instead.// This means that the following line outputs "A".alert(X[j]);

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...