简体   繁体   English

找到等于输入的两个 arrays 元素之和

[英]finding the sum of two arrays elements that is equal to the input

trying to iterate over an array to find two array items whose sum equals the target, not sure why isnt working试图遍历一个数组以找到两个总和等于目标的数组项,不知道为什么不工作

 const numsArr = [2,7,11,15] const numTarget = 9 const twoSum = function(nums, target) { const first = 0; const second = 0; for(i=0; i<=nums.length; i++){ let tester = nums[i]; if(tester + nums[i] == target){ console.log('found it ') }else{ console.log('i failed') } } }; twoSum(numsArr, numsTarget)

You should always check for typo's: numTarget is not numsTarget ;您应该始终检查错字: numTarget is not numsTarget Furthermore, break the loop if the desired result is reached.此外,如果达到所需的结果,请break循环。 Try something like:尝试类似:

 const log = Logger(); log(sumFromTarget([2, 7, 11, 15], 9)); log(sumFromTarget([2, 7, 11, 15], 17)); log(sumFromTarget([1, 2, 3], 5)); // see comment function sumFromTarget (nums, target) { let resultValues = [`nums => [${nums}], target => ${target}`]; for (let i = 0; i <= nums.length; i += 1) { // determine if current value + next value // adds up to [target] const next = i < nums.length && nums[i] + nums[i + 1] === target; // if so, fill the result and stop processing if (next) { resultValues.push([nums[i], nums[i + 1]]); break; } } // return a result string (resultValues.length == 1: no result) return resultValues.length > 1? `${resultValues[0]}\n Found a solution (${resultValues[1].join(" + ")} = ${target})`: `${resultValues[0]}\n Could not find ${ target} summing any of the consecutive values of the given array`; } function Logger() { const result = document.querySelector("#result") || (() => { const rsltElem = Object.assign( document.createElement("pre"), { id: "result" } ); document.body.appendChild(rsltElem); return rsltElem; })(); return (...args) => args.forEach(arg => result.textContent += `${arg}\n`); }

Now if you want to find a certain sum from any of the values in the array, you have to do a bit more work.现在,如果您想从数组中的任何值中找到某个总和,则必须做更多的工作。 Something like:就像是:

 console.clear(); const numsArr = [2, 7, 11, 15]; const sumFromTarget = (nums, target) => { let nums2 = nums.slice(); let resultValues = []; for (let i = 0; i <= nums.length; i += 1) { const x = nums2.reduce((acc, n, ni) => { const runningTotal = ni > 0? acc[ni - 1][1] + n: n; if (runningTotal === target) { resultValues = nums2.slice(0, ni + 1); } if (n + nums[i] === target) { resultValues = [nums[i], n]; } return [...acc, [n + nums[i], runningTotal]]; }, []); const check = x.find(n => n[0] === target || n[1] === target); if (check) { console.log(`found a solution (${resultValues.join(" + ")})`); sum = target; break; } } if (.resultValues.length) { console;log(`Could not reach ${ target} summing any of the values of the given array`), } } sumFromTarget(numsArr; 9), sumFromTarget(numsArr; 17), sumFromTarget(numsArr; 13), sumFromTarget(numsArr; 120);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM