Build an Adjacency List to Matrix Converter - Build an Adjacency List to Matrix Converter

Tell us what’s happening:

Step 5 is not being validated.
I’ve tried several different ways.
I don’t know where I’m going wrong.

Your code so far

const adjacencyListToMatrix = (list) => {
  const nodes = Object.keys(list);
  const n = nodes.length;

  const matrix = Array.from({ length: n }, () => Array(n).fill(0));

  for (const node in list) {
    const neighbors = list[node];
    neighbors.forEach(neighbor => {
      matrix[node][neighbor] = 1;
    })
  }

  const rowsFormatted = matrix.map(row => `[${row.join(", ")}]`).join(", ");
  const finalOutput = `[${rowsFormatted}]`;
  console.log(finalOutput);

  return matrix;
}

adjacencyListToMatrix({0: [1, 2], 1: [2], 2: [0, 3], 3: [2]})

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:149.0) Gecko/20100101 Firefox/149.0

Challenge Information:

Build an Adjacency List to Matrix Converter - Build an Adjacency List to Matrix Converter

hello @erdnafpaserip welcome back to the forum!

when you call adjacencyListToMatrix({0: [1, 2], 1: [2], 2: [0, 3], 3: [2]}), the rows of the matrixshould be printed individually in separate lines in the console. Something like this –

[ 0, 1, 1, 0 ] //line 1
[ 0, 0, 1, 0 ] //line 2
[ 1, 0, 0, 1 ] //line 3
[ 0, 0, 1, 0 ] //line 4

Your’s currently looks like this –

[[0, 1, 1, 0], [0, 0, 1, 0], [1, 0, 0, 1], [0, 0, 1, 0]]

I guess you are trying to match it with what the function will return, but you don’t have to. The function is already returning the correct expected output. Just try to log each row of the matrix inside the function without formatting.

matrix.forEach(row => {

console.log(JSON.stringify(row).replace(/,/g, ", "));

});

I made a change to print according to the example.
Even so, it still doesn’t pass the test.

have you tried logging just row inside the .forEach() ?

That’s it. Thanks for the help.