This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2011-08-25 15:33:49

ohaiderstudios
Scratcher
Registered: 2010-10-31
Posts: 100+

Another JavaScript Question

I'm having problems with this chunk of my QuickCode parser:

Code:

for (i = 0; i < blockList.length; i++) {
if (func1.test(blockList[i])) {
return true
}
else if (func2.test(blockList[i])) {
return true
}
else if (func3.test(blockList[i])) {
return true
}
else if (func4.test(blockList[i])) {
return true
}
else if (func5.test(blockList[i])) {
return true
}
else if (func6.test(blockList[i])) {
return true
}
else if (func7.test(blockList[i])) {
return true
}
else {
alert("The debugger returned an error on block: Line "+i+1)
return false
}
}

What happens, is that even if the length of the array blockList is greater than 1, the

Code:

for (i = 0; i < blockList.length; i++)

loop only loops around once. So even if the length of blockList is 7, the script only loops once.

This is the major bug that I am trying to deal with. Please help!


Fork Clamor on GitHub!

Offline

 

#2 2011-08-25 15:43:36

WindowsExplorer
Scratcher
Registered: 2011-02-25
Posts: 1000+

Re: Another JavaScript Question

I don't know lava script at all, but maybe put a forever loop in it?


http://i.imgur.com/H6LLdnK.pnghttp://i.imgur.com/VYuD7BY.png

Offline

 

#3 2011-08-25 15:45:35

bbbeb
Scratcher
Registered: 2009-06-11
Posts: 1000+

Re: Another JavaScript Question

hows about:

Code:

i > blocklist.Length

Back in my day.... there were no laws that censored the internet... now, there are.... nah.

Offline

 

#4 2011-08-25 22:18:09

ohaiderstudios
Scratcher
Registered: 2010-10-31
Posts: 100+

Re: Another JavaScript Question

bbbeb wrote:

hows about:

Code:

i > blocklist.Length

No, because the variable "i" starts out as 0, so if it only loops WHILE "i" is GREATER than the length of blockList, then it will still loop once, because no matter what the length of blockList, 0 can never be greater than it!

If you have any more suggestions, I'd love to here them! I'm completely confounded by this problem!!!!!  sad


Fork Clamor on GitHub!

Offline

 

#5 2011-08-26 03:11:31

TRocket
Scratcher
Registered: 2009-08-18
Posts: 1000+

Re: Another JavaScript Question

try

Code:

for (var i = 0; i < blockList.length; i++)

Last edited by TRocket (2011-08-26 03:12:23)


http://i.imgur.com/1QqnHxQ.png

Offline

 

#6 2011-08-26 07:07:14

roijac
Scratcher
Registered: 2010-01-19
Posts: 1000+

Re: Another JavaScript Question

you use the report function, which stoped the script  smile

happy to help!

Last edited by roijac (2011-08-26 07:07:24)

Offline

 

#7 2011-08-26 09:59:02

JSO
Community Moderator
Registered: 2007-06-23
Posts: 1000+

Re: Another JavaScript Question

return true will exit the current function. So maybe one of your test() functions is returning true?

Code:

function foo() {
    for (i=0; i<20; i++) {
        document.body.innerHTML += ('i is now ' + i + '<br>');
        if (i==10) {
            return true;
        }
    }
}

The above loop will only loop through 11 (0 to 10) iterations, even though it has "i<20" there.

Here's a running example I made:
http://jsfiddle.net/EnWRZ/

(click the "[•] Run" button)

Hope it helps!


http://oi48.tinypic.com/2v1q0e9.jpg

Offline

 

#8 2011-08-26 11:36:50

ohaiderstudios
Scratcher
Registered: 2010-10-31
Posts: 100+

Re: Another JavaScript Question

Thank you all so much  big_smile ! Now I've finally gotten around the main bug in my parser!


Fork Clamor on GitHub!

Offline

 

Board footer