[JavaScript] HashMap - Production ready code
\nvar numOfPairs = function(nums, target) {\n    const numCount = new Map();\n    let ans = 0;\n\n    for (const num of nums)\n        numCount.set(num, 1 + (nu
```\nvar numOfPairs = function(nums, target) {\n    const numCount = new Map();\n    let ans = 0;\n\n    for (const num of nums)\n        numCount.set(num, 1 + (numCount.get(num) || 0));\n\n    for (const [num, count] of numCount) {\n        if (target.startsWith(num)) {\n            const pair = target.substring(num.length);\n\n            if (num == pair)\n                ans += count * (count - 1);\n            else\n                ans += count * (numCount.get(pair) || 0);\n        }\n    }\n    \n    return ans;\n};\n```\n```\nvar numOfPairs = function(nums, target) {\n    let count = new Map(), totalPairs = 0;\n\n    for (const num of nums) {\n        if (num.length < target.length) {\n            totalPairs +=\n                (target.startsWith(num) && count.has(target.slice(num.length))\n                    ? count.get(target.slice(num.length))\n                    : 0) +\n                (target.endsWith(num) &&\n                count.has(target.slice(0, target.length - num.length))\n                    ? count.get(target.slice(0, target.length - num.length))\n                    : 0);\n\t\t}\n        count.set(num, 1 + (count.has(num) ? count.get(num) : 0));\n    }\n\n    return totalPairs;\n};\n```
[]