我在刷新应用程序时不时收到此错误消息,我正在建立一个反应难题,但找不到我所缺少或写错的东西吗?谁能帮我写些什么?
错误消息:TypeError: arr.reduce is not a function flattenArray
const flattenArray = arr => {
return arr.reduce((flatArr, subArr) => flatArr.concat(subArr), []);
};
代码:
const getShuffledPuzzle = () => {
const values = [0, 1, 2, 3, 4, 5, 6, 7, 8];
const rowOne = [],
rowTwo = [],
rowThree = [];
while (values.length) {
const random = Math.floor(Math.random() * values.length)
if (rowOne.length < 3) {
rowOne.push(values.splice(random, 1)[0])
} else if (rowTwo.length < 3) {
rowTwo.push(values.splice(random, 1)[0])
} else {
rowThree.push(values.splice(random, 1)[0])
}
}
return [rowOne, rowTwo, rowThree]
}
这是我收到TypeError的地方:
const flattenArray = (arr) => {
return arr.reduce((flatArr, subArr) => flatArr.concat(subArr), [])
};
const getInversionCount = arr => {
arr = flattenArray(arr).filter(n => n !== 0)
const inversions = [];
for (let i = 0; i < arr.length - 1; i++) {
const currentValue = arr[i]
const currentInversions = arr.filter(
(val, j) => i < j && val < currentValue
)
inversions.push(currentInversions.length)
}
const inversionsCount = inversions.reduce((total, val) => total + val, 0)
return inversionsCount
}
const isSolvable = puzzle => {
return getInversionCount(puzzle) % 2 === 0
};
const getPuzzle = () => {
let puzzle = getShuffledPuzzle()
while (!isSolvable(puzzle)) {
puzzle = getShuffledPuzzle
}
return puzzle
}
````
const flattenArray = (arr = []) => arr.reduce((flatArr, subArr) => flatArr.concat(subArr), [])