Nested for loop and multi-dimensional array question

Hello to all, i have a question about nested for loops. This is the code:

function eddardStarkSecret() {
	
var ans =[];
	var map = [
		[111, 1, 6], [110, 2, 5, 22], [115, 10], [83, 4], [119, 7],
		[105, 9], [97, 12, 15, 18], [84, 14], [114, 16, 19],
		[103, 17], [121, 20], [101, 21], [32, 3, 8, 11, 13], [74,0]
	]; 

	for (var i = 0; i < map.length; i++) {
		for (var j = 1; j < map[i].length; j++) 
			ans[map[i][j]] = map[i][0];
					
	}	
	return ans.reduce(function (prev, curr) {
		return prev + String.fromCharCode(curr);
	},"");
	
}

The ouput of the code is this: “Jon Snow is a Targaryen”

Now, my question is, how did it happen? I know how the array.reduce() part works. I am really confused how the nested for loop worked(like how did it produce the 32 charcode(space) and how did it make the array length 23 to produce the words need for the desired output). Your help would be very much be appreciated. Many. many thank you in advance ^_^.

The value 32 is already in the code, it isn’t being produced/computed

The length gets increased when a value is placed in the array at a higher index than currently exists

a = [];
a[3] = 'hello';
console.log(a);  // [ , , 'hello' ]

If you’re having trouble following what the nested loops are doing, try having a look at how ans gets changed as it runs

[ , 111, , , , , 111 ]
[ , 111, 110, , , 110, 111, , , , , , , , , , , , , , , , 110 ]
[ , 111, 110, , , 110, 111, , , , 115, , , , , , , , , , , , 110 ]
[ , 111, 110, , 83, 110, 111, , , , 115, , , , , , , , , , , , 110 ]
[ , 111, 110, , 83, 110, 111, 119, , , 115, , , , , , , , , , , , 110 ]
[ , 111, 110, , 83, 110, 111, 119, , 105, 115, , , , , , , , , , , , 110 ]
[ , 111, 110, , 83, 110, 111, 119, , 105, 115, , 97, , , 97, , , 97, , , , 110 ]
[ , 111, 110, , 83, 110, 111, 119, , 105, 115, , 97, , 84, 97, , , 97, , , , 110 ]
...
1 Like