[JavaScript] Easy to understand - loop & recursion
The strategy is straight forward. We just follow the rules and check length for every start value. Then we get the result.\nWe could modify the flag in visited 
The strategy is straight forward. We just follow the rules and check length for every start value. Then we get the result.\nWe could modify the flag in `visited` to avoid re-calculate for the set we met.\n\nHere\'s the code based on looping:\n\n```js\nconst arrayNesting = (nums) => {\n  const visited = new Uint8Array(nums.length);\n  let max = 0;\n  for (const num of nums) {\n    let cur = num;\n    let len = 0;\n    while (!visited[cur]) {\n      visited[cur] = 1;\n      ++len;\n      cur = nums[cur];\n    }\n    max = Math.max(max, len);\n  }\n  return max;\n};\n```\n\nHere\'s the code based on recursion:\n\n```js\nconst findPath = (nums, visited, cur, len = 0) => {\n  if (visited[cur]) return len;\n  visited[cur] = 1;\n  return findPath(nums, visited, nums[cur], len + 1);\n};\n\nconst arrayNesting = (nums) => {\n  const visited = new Uint8Array(nums.length);\n  let max = 0;\n  for (const num of nums) {\n    max = Math.max(max, findPath(nums, visited, num));\n  }\n  return max;\n};\n```\n\nAnd, we could even implement it in only 2 lines via `reduce`:\n\n```js\nconst findPath = (nums, visited, cur, len = 0) => visited[cur] ? len : (visited[cur] = 1, findPath(nums, visited, nums[cur], len + 1));\nconst arrayNesting = (nums, visited = new Uint8Array(nums.length)) => nums.reduce((max, num) => Math.max(max, findPath(nums, visited, num)), 0);\n```
['JavaScript']