我的 JavaScript 数组在对其应用排序函数后显示'undefined'。请协助



我正在尝试做我的第一次leetcode挑战。我非常不及格。我只是不明白为什么数组显示为未定义。我只是想整理一下。

我知道还有其他的方法来排序数组,但我似乎不能理解为什么我的排序方法在这里不工作。我希望你能解释一下。

编辑:我设法解决了未定义的问题,但现在似乎数组只存储一个值。有什么明显的我做错了吗?

编辑2:我认为数组只存储1单个值,因为我在这里使用"减少",这只是使用减少的性质似乎。我再试一次。谢谢你的帮助,伙计们:)

'use strict';

// define variables
let indexarray, outputarray = [];
const arr1 = [9, 4, 9, 8, 4];
const arr2 = [4, 9, 5];
function arrsort(array) {
const outputz = array.sort(function (a, b) { return a - b });
return outputz;
};

// sort arrays
let arr1s = arrsort(arr1);
let arr2s = arrsort(arr2);
// foreach in arr1, go through arr2 and check if current arr1 array value matches. if yes and current arr2 index val isnt present in 'indexarray', then add current arr2 array val to array 'outputarray' and current index val to 'indexarray'.
arr1s.forEach(function (x, i) {
arr2s.reduce(function (pval, cval, ival) {
if (cval === x && !indexarray.includes(ival)) {
indexarray.push(ival);
outputarray.push(cval);
};
});
});

更新-提交了如下所示的最终解决方案~noobhuzi

const intersect = function (nums1, nums2) {
// define variables
let indexarray1 = [];
let indexarray2 = [];
let outputarray = [];
const arrsort = function (array) {
return array.sort(function (a, b) { return a - b });
};

// sort arrays && 
let nums1s = arrsort(nums1);
let nums2s = arrsort(nums2);
/* foreach in nums1, go through nums2, check if nums1 val (x) matches nums2 val (pval). if yes and current nums2 index val isnt present in 'indexarray', then add current nums2 array val to array 'outputarray' and current index val to 'indexarray'. */
if (1 <= nums1s.length) {
nums1s.forEach(function (x, i) {
if (nums2s.length <= 1000 && 0 <= x && x <= 1000 && typeof x === "number") {
nums2s.forEach(function (pval, iv) {
if (pval === x && !indexarray2.includes(iv) && !indexarray1.includes(i)) {
indexarray1.push(i);
indexarray2.push(iv);
outputarray.push(pval);
}
});
} else {
console.log("ERROR: some constraint is stopping the code from continuing. please check around ForEach.");
};
});
} else {
console.log("ERROR: nums1s constraint is stopping the code from continuing as its length is less than 1.");
};
console.log("outputarray: " + outputarray);
return outputarray;
};

排序函数必须返回排序后的数组。indexarray的初始化也是错误的

你也不正确地使用Array.reduce。你只需要Array.forEach

更新答案。

// define variables
let indexarray = [];
let outputarray = [];
const arr1 = [9, 4, 9, 8, 4];
const arr2 = [4, 9, 5];
function arrsort(array) {
return array.sort(function (a, b) { return a - b });
};

// sort arrays
let arr1s = arrsort(arr1);
let arr2s = arrsort(arr2);
// foreach in arr1, go through arr2 and check if current arr1 array value matches.
// if yes and current arr2 index val isnt present in 'indexarray',
// then add current arr2 array val to array 'outputarray' and current index val to 'indexarray'.
arr1s.forEach(function (x, i) {
arr2s.forEach(function (pval, i2) {
if (pval === x && !indexarray.includes(i2)) {
indexarray.push(i2);
outputarray.push(pval);
};
});
});
console.log(indexarray);
console.log(outputarray);

相关内容

最新更新