Find your hat

Hi eveybody
I am trying to do find your hat project in javascript.
according to the first 2 assignments i need to initial a 2 dimantional array and print it to the console.
this is my code:

class Field {
    constructor(){
      this.field = [[]];
    }
   
    print(){
      console.log(this.field.join(''));
    }
  }
  
  const myField = new Field([[pathCharacter,fieldCharacter,hole],[fieldCharacter,hole,hole],[fieldCharacter,hat,fieldCharacter]]);
  myField.print();

the code isn’t right.
what is the right why of doing it?
thanks
Dorit

when creating a new instance of a class:

new Field()

you provide an argument:

[[pathCharacter,fieldCharacter,hole],[fieldCharacter,hole,hole],[fieldCharacter,hat,fieldCharacter]]

with this argument, the constructor is called:

constructor(){
   // body of your constructor
}

but your constructor doesn’t have any parameters. Thus you can pass arguments all you like to the constructor, they will have no effect.

ok, but what parameters should i write in the constructor?

given you have one argument (a multi-dimensional list) when creating a new class instance, a single parameter should do.

then you can set this.field to the parameter value, so you don’t end up with an empty list

example from MDN:

class Rectangle {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
}

rectangle = new Rectangle(5, 4);

thank you for the answer but what about the values of the 2 dimantional array?
where and how do i add the values?
Dorit

you have those here:

new Field([[pathCharacter,fieldCharacter,hole],[fieldCharacter,hole,hole],[fieldCharacter,hat,fieldCharacter]]);

once you made this array accessible through the instance field (this.field) in the constructor, you can access the array with all its values in the method using this.field like you currently do.

updated example:

class Rectangle {
  constructor(inputArray) {
    this.someArray = inputArray;
  }
}

rectangle = new Rectangle(['what', 'is', 'the', 'problem']);

if i where to define a method in this class, i could access the array through this.someArray

you use class Rectangle with two different constructors?

class Rectangle {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
}

and

class Rectangle {
  constructor(inputArray) {
    this.someArray = inputArray;
  }
}

sorry i cant understand what i am supposed to do…

the second example was closer to what you had to do.

Based on your previous posts, you worked with constructors before. Could you explain me what constructors are suppose to do? What purpose do constructors serve? I want to see how far you understand constructors.

Are you sure you’re practising the right thing here?
When learning something new it’s probably good to be standing on solid ground, to know 99% of what you are doing, and practising that 1%. When that’s been understood and solidified, then the next thing can be added on top.
If everything involved is unknown, then you have no way to reason about any of it and I’m not sure it’s possible to put any puzzle pieces in place when there aren’t any others nearby to fit them with.

It can be extremely difficult to answer questions when you’re missing all the firm ground that you’re supposed to be standing on, a bit like asking how to buy a burger before a restaurant has been built.

i understand that every class is supposed to have a constructor.
to initial the values i use an instance of the class using the new keyword.

i understand what you are saying, but i did the creditcard checker project and the mysterious organism project and this i the next one…

this statement is not true. Use a constructor when you have to

okay, that is good. What is then the struggle in your code to see that you don’t properly initialize the values in your constructor?

you pass a value to your constructor (when creating class instance), but your constructor has no parameter to handle this value.

lets go back to functions, when you have a function:

function example(){

}

example('abc');

i call the function, when i call the function, i provide an argument, but the function has no parameter, thus nothing happens to the value, i need a parameter:

function example(param){
    console.log(param);
}

example('abc');

same is true for methods and the constructor.

i’ll try to use this: class Rectangle { constructor(inputArray) { this.someArray = inputArray; } }
maybe i am just making things hard to understand
thanks, Dorit

class Field was perfectly fine, it was just showing how to use the constructor properly. Of course i used a different class name, otherwise you could just copy paste

i think you need to practice with constructors before rushing through the projects

You could use regular functions and objects instead of a class.
Instead of a constructor you would have a function that returns an object, and instead of methods you would have completely regular functions that accept that object as input, plus any other information they should receive.

To take the rectangle example, you could do this instead:

function Rectangle(height, width) {
  return { height: height, width: width }
}
function printRectangle(rectangle) {
  console.log(`A rectangle ${rectangle.height} high and ${rectangle.width} wide.`)
}
function makeRectangleTaller(rectangle) {
  // ... adjust the height and return the result
  // alternatively, modify the object and don't return anything
}

const myRectangle = Rectangle(3, 7)
const myOtherRectangle = Rectangle(8, 1)
printRectangle(myRectangle)
printRectangle(myOtherRectangle)

You could turn it into a class later once you’ve got all the logic down. That way you’d be solving one problem at a time.
And whenever something isn’t certain make sure that you fill that gap, go google it, find out, and only then proceed. So for example if function paramaters or return values aren’t the clearest thing in the world, then that would be a sign to stop what you’re doing, go figure out all about functions, and then come back and continue. If you continue anyway then I don’t imagine much of the rest will be easy to deal with.

Something that probably isn’t entirely obvious to new programmers is that the programmer needs to be in full control of what they’re writing, every single character in the code should be deliberate.

Another thing you can do is to read through all the instructions before you start, to get a good idea of what the overall thing is. Can’t really write code when you don’t know what you’re writing, so this helps more than you might think.

i will continue with the class, thanks for the advices.
i have a different problem now in the printing of the array, i can’t make it print the array in rows of ten.
i saved the values to a one dimantional array and don’t know how to print it to 2 dimantional.
any ideas someone?
thanks in advance
Dorit

If rows are 10 elements then you know that rows start at indices 0, 10, 20, … that’s just regular counting with increments of 10. And getting each cell would be counting upwards from the start.
You could also split it again, into chunks of 10, and print each chunk.
Mind you, this is every-day logic, you can make the same observations, if you were doing it with pen and paper you’d be doing the same thing so in a way you already have the answer to how to do it.

hi ionata
you are unfortanatly not understanding my questions and trying to make this questions not important.
if you see it this way, you can kindly not answer my questions.
the problem is that it prints every value in the array in a sparate row and i want it to be on the same row, it might sounds or really be an easy solution but for not for me, not right now.

Well, you did say that you had a one dimensional array and needed to split it.

Now it’s the opposite?

if you have many rows then you can get a single row by concatenating them.

> myRows
[[1,2,3,4,5,6,7,8,9,10],[11,12,13,14,15,16,17,18,19,20],[21,22,23,24,25,26,27,28,29,30]]
> concat myRows
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30]

what I’m saying is that you don’t need to consider any code when thinking about how to solve problems. how you would solve it is a better reference. yes, that makes a lot of problems easy. that still leaves writing the code, and that might not be easy, but it’s a whole lot easier after having decided what the code should do, that is the first part to solve.

hi everybody
i partialy found a solution to the printing matrix problem.
i used concat array to print the values in the same row but now for some reason it prints all the rows the same although i see that there are different values from the random function that are generating the array.
this is my code:
generatefield function:

static generateField(height,width,precentageholes){
    //generating a random function that put each time a different value and taking in account the holes precentage.
    
    let fieldArray= [,];
    let counter=0;
    for(let i=0;i<height;i++)
      {
      for(let j=0;j<width;j++)
        {
          const randomnumber = Math.floor(Math.random()*2);
          if(randomnumber===0)
            {
              fieldArray[i,j]=fieldCharacter;
            }
          else if(randomnumber===1)
            {
              fieldArray[i,j]=hole;
              
            }
        }
      }
   return fieldArray;
  }

print function:

 print(){
    for(let i=0;i<this.height;i++)
      {
console.log(this.myarray[i,0].concat(this.myarray[i,1]).concat(this.myarray[i,2]).concat(this.myarray[i,3]).concat(this.myarray[i,4]).concat(this.myarray[i,5]).concat(this.myarray[i,6]).concat(this.myarray[i,7]).
            concat(this.myarray[i,8]).concat(this.myarray[i,9]));
      //for(let j=0;j<this.width;j++)
       // {
         // console.log(this.myarray[i,j]);
      // }
      }
  }

thanks for your help
Dorit