Friday, June 29, 2018

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…

On the other hand the Algol68 code is a bit “OD”…

File: sum10.a68

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:
[ 1 ]
[ 2, 4 ]
[ 3, 6, 9 ]
[ 4, 8, 12, 16 ]
[ 5, 10, 15, 20, 25 ]
[ 6, 12, 18, 24, 30, 36 ]
[ 7, 14, 21, 28, 35, 42, 49 ]
[ 8, 16, 24, 32, 40, 48, 56, 64 ]
[ 9, 18, 27, 36, 45, 54, 63, 72, 81 ]
[ 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 ]
[ 11, 22, 33, 44, 55, 66, 77, 88, 99, 110, 121 ]
[ 12, 24, 36, 48, 60, 72, 84, 96, 108, 120, 132, 144 ]
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