What use is an array such as [0,1,2,3,4,5,6,7,8,9]

From some questions I have seen asked in forums, some people teaching JavaScript are giving homework questions to their students where they ask them to create an array where each position in the array contains the number corresponding to that position in the array. Now of course with only a few numbers you can easily hard code such an array - for example [0,1,2,3,4,5,6,7,8,9] so they either specify a much larger number or ask for the code to create the array to be flexible.

From the way such questions are asked it is obvious that the intention is for the student to write a FOR loop to populate their array.

As a reasonably experienced JavaScript programmer, I know that FOR loops are not often needed any more in JavaScript and this particular example seemed particularly pointless except for showing howFOR loops are coded.

I then got to wondering whether such an arraywhere each element in the array contains the number corresponding to its position in the array would ever be actually useful . Then it came to me that you can use such an array to completely eliminate the need for the FOR loop in JavaScript.

So ironically these classes are using an example for learning how to write FOR loops where the resultant array in fact completely eliminates the need to use FORloops in JavaScript.

So what’s the purpose of my posting this? Well I was wondering what other examples are being used to teach JavaScript where an obvious use for what the code constructs is to eliminate the need for the code they are teaching. Have you come across any examples of where beginners are being taught a particular JavaScript command where what their code is supposed to be making can actually eliminate the need for that command?

Just to make it clearer what I mean about being able to use such an array to eliminate the need for FOR loops, here’s code that uses such an array to create a multiplication table that shows the results of multiplying any two numbers of 20 or less together (that can easily be changed to show multiplication results up to any number simply by changing the size of the array you are populating - which is passed to the function).:

Array.prototype.populate = function(n) {
return Object.keys(Object(0+Array(n)));
};
(function(sz) {
"use strict";
var tabl, row, ar;
ar = [].populate(sz+1).shift();
tabl = document.createElement('table');
tabl.createTHead();
row = tabl.tHead.insertRow(-1);
row.insertCell(0); row.cells[0].innerHTML = '*';
ar.forEach(function(a) {row.insertCell(a); row.cells[a].innerHTML = a;});
tabl.appendChild(document.createElement('tbody'));
ar.forEach(function(a) {
   row = tabl.tBodies[0].insertRow(-1);
   row.insertCell(0); row.cells[0].innerHTML = a;
   row.cells[0].className = 'first';
   ar.forEach(function(b) {row.insertCell(b); row.cells[b].innerHTML = b*a;});
});
document.getElementById('mult').appendChild(tabl);
})(20);

very nice teaching example :slight_smile:
thanks for sharing


Array.prototype.populate = function(n) {
return Object.keys(Object(0+Array(n)));
};
(function(sz) {
"use strict";
var ar = [].populate(sz+1);
console.log(ar);
})(20);