我想按从高到低的顺序组织randomNumber中的数字(包括重复项)并将其插入OrganizedNumbers中。
我认为这是我的while循环抛出它,但是我不明白为什么。
let randomNumbers = [9, 2, 6, 8, 52, 56, 7, 43, 56, 31, 48, 3, 6, 8, 4, 1, 3, 4, 5, 17, 25];
let organisedNumbers = [];
function unjumbleTwo(sourceArray, resultArray) {
for (let i = 0, next = false; i < Math.max(...sourceArray); next === true) {
next = false;
let temp = sourceArray[sourceArray.indexOf(i)];
while (typeof temp === 'number') {
resultArray.push(temp);
}
next = true;
}
};
unjumbleTwo(randomNumbers, organisedNumbers);
console.log(organisedNumbers);
编辑:“ i”从未更改。已修复并摆脱了“下一个=== true,false”。
function unjumble(sourceArray,resultArray){
for (let i = 0; i < Math.max(...sourceArray); i++){
let temp = sourceArray[sourceArray.indexOf(i)];
while (typeof temp === 'number'){
resultArray.push(temp);
}
}
};
unjumble(randomNumbers,organisedNumbers);
console.log(organisedNumbers);
答案 0 :(得分:-1)
就这样返回它:
randomNumbers.sort((a,b) => a > b ? 1 : a < b ? -1 : 0);
答案 1 :(得分:-1)
您可以使用排序功能:
let randomNumbers = [9,2,6,8,52,56,7,43,56,31,48,3,6,8,4,1,3,4,5,17,25];
let organisedNumbers = randomNumbers.sort((a, b) => b - a);
console.log(organisedNumbers)