+ 1

Can a div element be created using JS then assigned to an existing class using a constructor function?

I am trying to do this for my Match Three game to create new pieces without having to copy and paste the code from fillBoard to re-assign the properties such as locID and colorID. https://code.sololearn.com/WIGYx83rvJhh/?ref=app

31st Jan 2022, 4:58 PM
Margaret Guzman
Margaret Guzman - avatar
2 Respostas
+ 1
Love that you're still going at this, awesome dedication. You could create a function constructPiece(p, locId) { p.locId = locId; ... switch (colorId) .... } which does all the code that's currently in fillBoard, but only for a single piece. Then the fillBoard function is reduced to for(var x = 0; x < 25; x++) { var p = document.createElement("div"); constructPiece(p, x); } And of course you can also reuse the `constructPiece` function someplace else. :)
1st Feb 2022, 1:14 AM
Schindlabua
Schindlabua - avatar
31st Jan 2022, 5:07 PM
CGM
CGM - avatar