-
• #2
there is no return value from peekVal(), return value from method passed to setTimeout is something completely unrelated
https://stackoverflow.com/questions/24928846/get-return-value-from-settimeout
and BTW, I wouldn't consider that as 'simple piece of code', looks more like opposite of that :-)
-
• #3
Fri 2021.05.28
'now, all I want is to be able to "see"(print) that value-position'
Place L11
analogRead(A5)
inside a small snippet, similarly to this example using an accelerometer:ref: ' I've only just pick up JS w/ Espruino and I'm kind of struggling'
Okay, @GeorgeM you did clarify indicating new to Espruino, does that include the interpreter language Javascript as well?
The understanding of 'undefined' needs to be revisited.
'that it's printing undefined because it calls for the print much faster than it takes the function to complete'
'(at least I'm pretty sure that's what's going on)'
The ninth bullet item down the list at:
http://www.espruino.com/FAQ
will take you to:
http://www.espruino.com/FAQ#when-i-type-a-command-why-does-espruino-print-undefined-Espruino follows the convention established at MDN:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined
See explanation below heading DescriptionOther useful resource:
-
• #5
Sat 2021.05.29
Although this might be more of a user/author preference, I was taught one path for data in, one path for data out, otherwise there will come the day the trap will spring. Under some circumstances, it may be prudent to have more than one return statment within a function, such as trying a quick hack to test some other code block, but I think you will find that, that technique will lead to more errors and headaches over time.
ref: L25 L37 L43 return
Another useful tool using the WebIDE should than not be discovered yet. As simple as adding the line 'debugger;' as the breakpoint, then single step through or roll-over each variable to reveal it's value.
Right, just so it's out the way... I've only just pick up JS w/ Espruino and I'm kind of struggling with a simple piece of code:
All I'm trying to do atm is to read a value from a POT(mimicking a future sensor) which then gets stored in an array depending on the two inputs which then would get the position in that array of the greatest value stored... now, all I want is to be able to "see"(print) that value-position, then store it under another const/ let and do something with it...
In Python this would be something simple like:
Now the printing issue is kind of solved in the sense that I can just print the return of that conditional:
But if I'm doing it from where I'm calling the function it's giving me "undefined"... now I get the fact that it's printing undefined because it calls for the print much faster than it takes the function to complete and output something out(at least I'm pretty sure that's what's going on)... but just as a test I've tried using a setTimeout on the print of about 3sec and I'm still getting undefined back which only confused me even more...