Easy to understand code with detailed explanation
Approach\nThe problem requires us to determine the minimum number of cables that need to be added to an existing network of computers to make all the computers 
# Approach\nThe problem requires us to determine the minimum number of cables that need to be added to an existing network of computers to make all the computers connected. If this is not possible, we need to return -1.\n\nThe solution follows a simple algorithmic approach:\n\n1. Check if the number of cables in the network is less than the minimum required to connect all the computers. If yes, return -1 since it is not possible to connect all computers.\n\n2. Create a graph data structure from the existing network by iterating through the connections and creating an adjacency list of neighbors for each computer. Since the connections are bi-directional, we add each computer to the other computer\'s adjacency list.\n\n3. Use Depth First Search (DFS) to traverse the graph and count the number of separate networks. We initialize a count variable `networkCount` to 0, and we traverse the graph starting from each unvisited computer node. For each unvisited node, we mark it as visited, then traverse its neighbors recursively using DFS. The DFS traversal will cover all the connected nodes in **a particular network**. After the traversal is complete, we increment `networkCount` to indicate that we have discovered a separate network.\n\n4. Return `networkCount - 1` since the minimum number of cables required to connect all the computers equals the number of networks in the graph minus one. This is because each cable connects two computers, and we need to have a single network, so we need `networkCount - 1` cables to connect all the separate networks.\n\n## JavaScript Code\n```\nvar makeConnected = function (n, connections) {\n  // If number of connections is less than required to connect all the nodes\n  // then it\'s not possible to connect all computers\n  if (connections.length < n - 1) return -1;\n\n  // create an empty object to store the neighbors of each node\n  let neighbors = {};\n\n  // create an array to track whether each node has been visited during traversal\n  let isVisited = Array(n).fill(false);\n\n  // initialize each node\'s neighbors array to be empty\n  for (let node = 0; node < n; node++) neighbors[node] = [];\n\n  // Loop through each connection and add each node to the other\'s list of neighbors\n  for (const connection of connections) {\n    neighbors[connection[0]].push(connection[1]);\n    neighbors[connection[1]].push(connection[0]);\n  }\n\n  // initialize a counter for the number of separate networks in the network\n  let networkCount = 0;\n\n  // loop through each node in the network\n  for (let node = 0; node < n; node++) {\n    // if the node has not been visited yet, traverse its network of connected nodes\n    if (!isVisited[node]) {\n      traverseNetwork(node);\n      // increment the network count because we have found another separate network\n      networkCount++;\n    }\n  }\n\n  // The number of additional connections needed to connect all computers is equal to the\n  // number of networks found minus 1 (since one connection connects 2 networks)\n  return networkCount - 1;\n\n  // This is a recursive function that performs a depth-first search traversal of the network\n  // starting at the given source node. It marks each visited node as visited in the isVisited array.\n  // The purpose of this function is to find all the nodes that are connected to the same network.\n  function traverseNetwork(sourceNode) {\n    isVisited[sourceNode] = true;\n\n    // For each neighbor of the source node, if the neighbor has not been visited,\n    // perform a recursive traversal from the neighbor node.\n    for (const neighborNode of neighbors[sourceNode]) {\n      if (!isVisited[neighborNode]) {\n        traverseNetwork(neighborNode);\n      }\n    }\n  }\n};\n```
['Depth-First Search', 'Graph', 'JavaScript']