offline version 0.0.2


Discussion of the find the number game in JavaScript

Let's now discuss the steps in solving our problem. Obviously, first we need to write code that will generate a table of a given size filled with random numbers.

However, this subtask is too large. Let's break it down into smaller ones.

It seems to me convenient to make a function that will accept a reference to the playing field and an array with random numbers as parameters, and will form table cells, populating them with numbers from the array:

function build(field, arr) { }

It would be convenient if the array passed to this function would be two-dimensional, already specially prepared, with random numbers.

Let this array be prepared by the prepare function, which receives the size of the playing field side as a parameter:

function prepare(size) { }

Using our functions, we can create an initial 2 by 2 table like this:

build(field, prepare(2));

The prepare function will be too complicated if it contains all the code for preparing the array. It would be better if it uses helper functions.

Let the helper function range form an array with numbers from 1 up to the maximum number of the table:

function range(count) { }

Let the shuffle function shuffle the array elements randomly:

function shuffle(arr) { }

Let the chunk function split a one-dimensional array into a two-dimensional one. Let the second parameter indicate the number of elements in the subarray:

function chunk(arr, n) { }

Having such functions, we can use them to make the prepare function we need:

function prepare(size) { let arr = []; arr = range(size * size); arr = shuffle(arr); arr = chunk(arr, size); return arr; }

Make the described range function. Test its work.

Make the described shuffle function. Test its work.

Make the described chunk function. Test its work.

Test the prepare function you created.

enru