Uncaught SyntaxError: BREAK statement outside of SWITCH, FOR or WHILE loop #6052
Replies: 1 comment
-
Posted at 2018-10-02 by Robin Mon 2018.10.01
Hello @user94507, your post is timely. I just experienced the same error within a nested for loop only an hour ago. e.g. two for loops. Break in same position just after the nested inside loop. You have saved me the effort of creating this original post.
Posted at 2018-10-02 by @allObjects Give this a shot:
...there were obviously also some other issues... - see What is the sort helping with? ...do you have duplicate ssid in scan result and want only the one to match w/ highest rssi value? (Since labels are bad in general and worst in compiler free environment - as Espruino - in regard of nested control management, labels are not implemented in Espruino. Why though the simple, one-level break is not working... I don't know). Posted at 2018-10-02 by @gfwilliams Actually I just tried this:
And it breaks on 1v99 but works on cutting edge - so I'd just update your firmware. I will do a proper numbered release this week anyway hopefully. With the new firmware you also have the option of using Not tested, but you get the idea:
Posted at 2018-10-02 by fanThomas Thanks for your tips, you are right there are some other issues. I came not across this yet because I got this compilation error which was irritating me. The mentioned error message does not go away, even with the corrected code. Of course I can remove the the breaks, but the code is not efficient because it does not stop looping when the correct record is found. Posted at 2018-10-02 by @allObjects @user94507, including @gfwilliams, neat, it's like a find all, where @user94507 was looking for a find first. With the built-in Posted at 2018-10-02 by fanThomas @allObjects ah ya! i have overseen this in your suggestion! Brilliant! The way around beak is perfect. Thank you. @gfwilliams I have tried using the function Array.filter() replacing the inner loop. This works although I must pass a parameter to this function and so the Web Ide warns me "don't use a function within a loop". Posted at 2018-10-02 by @allObjects @user94507, this should work too - notice the switching of inner w/ outer loop.
It still loops through all elements of the outer array (aMyAP[]), but stops comparing and looping through though the inner array (aAP[]) on first find (match). Both the If you do not like the looping through the first one and have even the optional telling where to start looping including handling of undefined and null arrays, you can use this findFirstIn(arr, mchr, strt, endx):
returns first element for which 'mchr(element)' return true, else undefined
Posted at 2018-10-03 by @allObjects And here is the test... (cannot stand code that has not been tested or at least has run once...).
Code executes immediately on upload... since it is in level 0... not recommended (by me, especially not when it contains some async / callback and communication stuff, like connect to AP / Wifi)... all level 0 code - code that is executed on uploade / on reception by the Espruino board should always all go into an appInit() method that is called from the onInit() method. But in this (test code) case, it works... at least my (1v3 HW Version) PICO. You can play with the If you want to track visually what is going on, set
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Posted at 2018-10-01 by fanThomas
I have a nested while construct to find a value found in a source array to be within a destination array. When found want to break out from both loops. But it throws an error on the break statement in the outer loop. Could this be an error or do I miss something?
I am using Espruino 1v99 on a ESP2866
Here is the code:
Beta Was this translation helpful? Give feedback.
All reactions