0

I need an explanation to as what has been done in the gridFull state of this constructor.Being new to javascript, I couldn't get this line of code.

constructor() {
        super();
        this.speed = 100;
        this.rows = 30;
        this.cols = 50;

        this.state = {
            generation: 0,
            gridFull: Array(this.rows).fill().map(() => Array(this.cols).fill(false))// this line of code is unclear to me
        }
5
  • Where did you get this code? What is the context / what is it supposed to be doing? Commented Jan 24, 2018 at 19:52
  • Which part of the code don't you understand? Have you tried Googling each of the various methods such as .fill() and .map() individually? Commented Jan 24, 2018 at 19:53
  • i got this code from github.com/beaucarnes/fcc-project-tutorials/tree/master/… Commented Jan 24, 2018 at 19:53
  • i understand what .fill and .map does does in javascript but I am quite puzzled at how they have been used together alongwith an arrow function. Commented Jan 24, 2018 at 19:57
  • 1
    @satyajeetjha Note how the answer breaks the line of code you are asking about into small pieces. Descriptions for each of these pieces can be found with some research, usually starting with Google. Breaking things into smaller pieces like this is a critical skill as you continue your journey to learn about programming. Good luck! Commented Jan 24, 2018 at 20:00

1 Answer 1

1

Let's break down the line:

Array(this.rows)

this creates an array with this.rows many rows. In this case, 30.

.fill()

fills the array with undefined values (more info on fill function)

.map(callbackFunction)

this returns a new array with each value being transformed by the function. Since you have an array of undefined, you'll call the function as you would the following callbackFunction(undefined).

Now for the callback function:

() => Array(this.cols).fill(false);

This function takes no parameters (hence ()), and returns an Array with this.cols size (which is 50), all containing false.

tl;dr: So, you're essentially creating a 30x50 matrix filled with false on each element.

EDIT:

explaining arrow functions:

(list-of-parameters) => (function block | return value)

To explain using examples, we can transform function one() { return 1; } into () => 1;.

Or function times(a, b) { return a * b;} into (a, b) => a * b;

Or another:

let x = 0;
function foo(y) {
  const returnValue = (x++) * y;
  return returnValue;
}

to

let x = 0;
const foo = (y) => {
  const returnValue = (x++) * y;
  return returnValue;
}

EDIT2:

More ways to accomplish the same result:

let result = Array(rowCount).fill();
for (let i = 0; I < rowCount; i++) {
  result[i] = Array(colCount).fill(false);
}

Another:

const line = Array(colCount).fill(false);
const result = Array(rowCount).fill().map(() => [...line]);

And another:

const line = Array(colCount).fill(false);
const result = [];
for (let idx = 0; idx < rowCount; idx++) {
  result.push([...line]);
}

Or you can create your own "matrix creator":

function matrix(row, col) {
  const data = Array(row * col).fill(false);

  const findIdx = (x, y) => y * col + x;

  return {
    get: (x, y) => data[findIdx(x,y)],
    set: (x, y, value) => {
      data[findIdx(x,y)] = value
      return data[findIdx(x,y);
    },
  };
}
Sign up to request clarification or add additional context in comments.

2 Comments

.This helped.Can you give few more examples to illustrate this concept?
I've added more examples. I don't know if that's what you're looking for. Let me know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.