Just yesterday I had to try an explain javaScript`s “for” loop to
an junior trying to learn javaScript coding during the school holidays…
So we trialled adding up all the whole numbers from 1 to 100! The code
is simple enough…
File: sum10.js
sum100=0;
for(i=1; i<=100; i+=1){ sum100+=i; };
console.log(“Sum of all numbers from 1 to 100 is:”,sum100)
Now I do like anchovies and olives on my Neapolitan pizza … most kids
don`t… so (to me) the above Javascript code is REALLY yummy, especially
“for(i=1; i<=100; i++)…;” and wonderfully loaded with possibilities
and flavour… I can almost smell the code a “C” compiler would generate…
INT sum100:=0;
FOR i FROM 1 BY 1 TO 100 DO sum100+:=i OD;
print((“Sum of all numbers from 1 to 100 is:”,sum100))
But ignoring the “OD” we still get “FOR i FROM 1 BY 1 TO 100 DO … OD”
… Which I find totally vanilla, and deceptively readable… Maybe even
boring.
With JS and a junior, I found it really surprising the number of ways
a “newbie” can get the “for(i==1, i<100, i+=i)” code wrong… If you
dont believe me, find a code fearing 11th-grader and ask them generate
the following primary school multiplication triangle:
As a result, I also needed to tutor (and demonstrate) the difference
between “curly brackets”, “round brackets” and “square brackets”…. as I
said at the top, I like anchovies and olives, part of the fun of
discovering a new Neapolitan pizza, but we should not inflict our
acquired tastes on juniors.
I finish with a quote by Daniel Klein: “The only thing that separates us from the animals is superstition and mindless rituals.” (also attributed to – Latka Gravas (Andy Kauffman), Taxi)
File: sum10.js
Now I do like anchovies and olives on my Neapolitan pizza … most kids don`t… so (to me) the above Javascript code is REALLY yummy, especially “for(i=1; i<=100; i++)…;” and wonderfully loaded with possibilities and flavour… I can almost smell the code a “C” compiler would generate…
On the other hand the Algol68 code is a bit “OD”…
File: sum10.a68
But ignoring the “OD” we still get “FOR i FROM 1 BY 1 TO 100 DO … OD” … Which I find totally vanilla, and deceptively readable… Maybe even boring.
With JS and a junior, I found it really surprising the number of ways a “newbie” can get the “for(i==1, i<100, i+=i)” code wrong… If you dont believe me, find a code fearing 11th-grader and ask them generate the following primary school multiplication triangle:
As a result, I also needed to tutor (and demonstrate) the difference between “curly brackets”, “round brackets” and “square brackets”…. as I said at the top, I like anchovies and olives, part of the fun of discovering a new Neapolitan pizza, but we should not inflict our acquired tastes on juniors.
I finish with a quote by Daniel Klein: “The only thing that separates us from the animals is superstition and mindless rituals.” (also attributed to – Latka Gravas (Andy Kauffman), Taxi)
ps. the “DO ~ OD” blocks I call “Guarded Blocks” inspired by Dijkstra around 1974. cf. https://www.cs.utexas.edu/users/EWD/transcriptions/EWD04xx/EWD472.html