Closed JocelynDelalande closed 6 years ago
This is part of JavaScript itself. If you try the same in the node
console or in devtools for Chrome or Firefox, you'll get the same:
REPLs show the last result of the code. What's peculiar is that JavaScript while loops and blocks have a result. The result of a block is the result of the last statement in the block ({1; 2}
--> 2
). The result of a while statement is the result of the last iteration of the statement.
In case you were curious, the same applies to for
loops too:
@rgbkrk ok, very interesting.
So my only (reasonable) way here seems to be disabling the display of all execution results, I have to search on that.
I left this issue open for a few days in case someone comes with something better.
@JocelynDelalande to avoid this JS behaviour, you could rewrite your code like this:
var i = 0;
while(i < 10) {
console.log(i++);
}
Nicolas Riesco notifications@github.com wrote:
@JocelynDelalande to avoid this JS behaviour, you could rewrite your code like this:
var i = 0; while(i < 10) { console.log(i++); }
@n-riesco Thanks, but for a gentle introduction to algorithmics, this is pretty hardcore stuff :-).
@JocelynDelalande Oh, I see. In that case, I'd use a for
-loop:
for(var i = 0; i < 10; i++) {
console.log(i);
}
@n-riesco yes, but in this very case, my point si to show/experiment the while
loop.
I'm trying to adapt the tools for the teaching, not the teaching to to the tools :-)
@n-riesco any idea/lead on how to disable the display of the latest evaluated expression in ijavascript notebooks ?
@n-riesco yes, but in this very case, my point si to show/experiment the while loop.
I'm trying to adapt the tools for the teaching, not the teaching to to the tools :-)@n-riesco any idea/lead on how to disable the display of the latest evaluated expression in ijavascript notebooks ?
The idea behind the examples I've given above is to ensure the last statement returns undefined
(by default ijsinstall
installs the IJavascript kernel with the flag --hide-undefined
).
I could implement a flag to hide execution results (e.g. --hide-execution-result
). I don't see any harm with such an option.
Alternatively, one could develop an nbextension to hide execution results (like https://github.com/ipython-contrib/jupyter_contrib_nbextensions/tree/master/src/jupyter_contrib_nbextensions/nbextensions/hide_input_all). This approach would work regardless the kernel.
@n-riesco great, thanks :-)
@JocelynDelalande I haven't published a new release in npm
yet. I wanted to update IJavascript's documentation before making a new release. But, if you're a going to use this feature straight away, I could make a release today.
I'm fine, I'll maybe use it in several months :-)
See that example:
I understand that ijavascript outputs the value of the latest affected variable (so displaying 11, which is the value of
i
).But in the context of teaching programming basics, this is very confusing ("why does it counts till 11 instead of 10 ?" ).
So…
Short-term question
Is there a way to disable this behavior ?
Long-term question
Is this behavior desirable at all ?
FYI, ipython python core does not have this behavior:
Thanks by advance :-)