It is a simple exercise that I am doing for mere practice and leisure, I have done it in various ways but I was wondering if there is an even more practical way or to reduce the lines of code making use of the many methods of JavaScript.
The exercise is about receiving an array (arr) and a number (target) and returning another array with a pair of numbers found in 'arr' whose sum is equal to 'target'.
function targetSum3(arr, target) { let newArr = []; let copyArray = arr; for (let i of copyArray) { let x = Math.abs(i - target); copyArray.pop(copyArray[i]); if (copyArray.includes(x) && (copyArray.indexOf(x) != copyArray.indexOf(i))) { newArr.push(i); newArr.push(x); return newArr; } } return newArr; }