let arr = [1,2,6,6,7,8,11,12,13,4,4] let num = 12 function solution(arr, target) { console.log('给出指定数组--->', arr) console.log('给出指定数--->', target) const list = []; if (arr.length > 1) { for (let i = 0; i < arr.length; i++) { const val = target - arr[i]; const index = arr.slice(i + 1).findIndex(i => i === val) if (index !== -1) { list.push([arr[i],val]); } } console.log('两两相加等于指定数的两个数:', list) return list } } console.log('一共有多少对这样的数:', solution(arr, num).length)
标签:arr,固定值,console,log,val,javascript,list,对数,const From: https://www.cnblogs.com/zhixi/p/16881284.html