Can't explain this interview question

neo was given a pill, and he was born special.

i’m serious, how do you do it? i have absolutely no clue how to work with code, without a browser.

A potential fix:

const defaultValue = 0;
function defaultMatrix(size) { // returns array
	// fix me!
	let row = [];
	let matrix = [];
	for (let i=0; i < size; i++) { row = [...row, defaultValue]; } // might be overkill
	for (let i=0; i < size; i++) { matrix[i] = [...row]; } // the magic
	return matrix;
}

I believe that the function is very verbose and can be fixed better using Array.from, but that’s for another discussion

Did you read the inline comment @camperextraordinaire??

OK fair enough. Thank you

Hi,

I share my solution because personally I believe that row variable isn’t necessary. It is just one solution from the ton of. So:

function defaultMatrix(size) {
	var defaultValue = 0;
	var matrix = [];
	
	for (var i=0; i < size; i++) { matrix.push(new Array(size).fill(defaultValue)); }
	return matrix;
}

Actually, if we want to do a default matrix and we insist zero is as default value, the defaultValue variable is not necessary, too. From my perspective, of course.

How to fix that properly?