Kevin Powell - CommunityKP-C
Kevin Powell - Communityโ€ข5mo agoโ€ข
9 replies
brr

Array().fill acts weirdly

// Option 1
    function initialize() {
        let matrix = new Array(3).fill(["-", "-", "-"]);
        return matrix;
    };
// Option 2
    function initialize() {
        let matrix = new Array();
        for (; matrix.length < 3;) {
            let row = ["-", "-", "-"];
            matrix.push(row);
        };
        return matrix;
    };

If I do matrix[0][0] = "a" in option 1 matrix, it replaces entire 1st column with "a". But that's not the case for option 2, it only only replaces at the specified position.
Was this page helpful?