Jump to content

Loops Making Me Go Loopy


chibineku

Recommended Posts

While DD is writing a response, you can check out this link for an explanation.http://pietschsoft.com/post/2008/02/JavaSc...Equivalent.aspxEDIT: An even better link: http://en.wikipedia.org/wiki/Foreach
Ah. So the explanation is pretty much, "It's like that, and that's the way it is!" Neat-o.I am waiting on DD's reply on a slightly different subject than this, to do with splicing. I am nearly there in my understanding of it, but it doesn't quite do what I want yet, for reasons that are beyond me.
Link to comment
Share on other sites

I suspect maybe chibineku is confused about the for ... in loop. The usual for loop is simply a way of executing a group of statements a certain number of times, or until some condition is met. The for ... in loop exists for the specific purpose of stepping through the elements of an object, such as an array. It's advantage is that it works on named elements as well as numerically-indexed elements. In every iteration, x (or whatever you choose to name it) returns the name/index of the next element, which means that object[x] returns the value of that element.The real advantage comes when you don't know ahead of time what the names of the elements are.EDIT. I'm still supposed to answer something about splicing? :)If you're using JSG's technique, which I recommend, you shouldn't even need to be splicing. You're storing the results in an array, now, right? The splicing idea was never going to work in the long run. As soon as you count 10 of the same character, you've broken your results string, because the indexes are all offset in unpredictable ways.

Link to comment
Share on other sites

EDIT. I'm still supposed to answer something about splicing? :)
I am still having one or two problems, and I reposted a big chunk of code yesterday (possibly it's on the first page of this thread) begging more help. It shows what my current script looks like but it doesn't display answers corrently. It might not be worth checking now, because I want to try adapting it to create the output string more neatly, like the other two solutions (justsomeguy's and Ingolme's). I'm not saying your solution was messy, just that I supplied code with a horrible way of creating the output string.
Link to comment
Share on other sites

I kept editing after you posted. Look up two posts.
Ah, ok. Well, it's still a good thing to know, perhaps for future applications. jsg's solution is the sleekest anyway. Thanks again to all who have given their input to this thread :)
Link to comment
Share on other sites

How does x come to recieve a value
The easiest English-language description for something like this:for (x in a)is:"for each value in a, assign the value to x and run the loop"That's just how the for..in loop works. The reason I wrote var before the variable name:for (var x in found_chars)is because up until that point x was undefined, so using var in that line will actually define x the first time through. You can also define a variable first and then use it (it will lose whatever value it had and run through the array like normal):var x = 10;for (x in found_chars)Javascript doesn't require that you define variables before you use them, but it's a good practice to do so. The PHP equivalent is a foreach loop:foreach ($array as $x)or foreach ($array as $key => $value)
Link to comment
Share on other sites

The easiest English-language description for something like this:for (x in a)is:"for each value in a, assign the value to x and run the loop"That's just how the for..in loop works. The reason I wrote var before the variable name:for (var x in found_chars)is because up until that point x was undefined, so using var in that line will actually define x the first time through. You can also define a variable first and then use it (it will lose whatever value it had and run through the array like normal):var x = 10;for (x in found_chars)Javascript doesn't require that you define variables before you use them, but it's a good practice to do so. The PHP equivalent is a foreach loop:foreach ($array as $x)or foreach ($array as $key => $value)
Arigato gozaimasu :)One more thing relating to objects, more particularly to arrays, though (arrays are objects, right?):In the Javascript Bible, there is a section on creating navigation bars using JS (and using predominantly CSS), and in the JS version there is a variable called currOffset, which Goodman (the author) uses to keep track of what page you're currently on (so that buttons 'prev' and 'next' know where to go, and if there aren't any more pages). Easier shown than explained:
function nextPage() {var currOffset=parent.currPage;if (currOffset<5) {document.getElementById("thispage").innerHTML=currOffsetcurrOffset++parent.logoArea.location.href="review"+currOffset+".html";parent.reviewArea.location.href="review"+currOffset+".html";} else {alert("There aren't any more at the moment - check back soon for more!");}}

The prevPage function does the same, but takes one from the value of currOffset. The odd thing is, that the original function adds one to the value of currOffset using +=, instead of ++. It doesn't work like that, so I altered it. It may be a typo, but then the function that subtracts one uses -=,which shouldn't work, especially since I haven't used += in the next page function. But it does. Weird, no?Kind of unrelated, sorry, but I don't want to start a new thread for such a little query.

Link to comment
Share on other sites

+=, -=, *=, etc. are all assignment operators. They require a value on the right side as well as the left side, as in this:x = 5;x += 5;// x returns 10++ and -- are increment and decrement operators and only require a value on the left side of the operator:a = 1;a++;// a returns 2They are not interchangeable. The author might have changed his intention while writing, but didn't make a complete change in the code. Or as you say, it could be a typo. Code must be a nightmare to proofread.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...