使用JavaScript数组arrList完成下列需求 (算法编程题,完成度越高,得分越高)
先将每个奇数存到oddRepeat数组中,将每个偶数存到evenRepeat数组中(数字不去重,出现几次写几次)
再对整个数组进行去重并执行排序操作(数字从大到小排序,不允许使用JavaScript内置函数sort实现)
最后,将排序后的数据保存到数组list中
最终结果如下所示:
var arrList = [1, 9, 0, 2, 1, 6, 8, 11, 0, 5, 18, 1, 26, 11, 8, 2, 3, 5, 18] { list: [ 26, 18, 11, 9, 8, 6, 5, 3, 2, 1, 0 ], evenRepeat: [ 0, 2, 6, 8, 0, 18, 26, 8, 2, 18 ], oddRepeat: [ 1, 9, 1, 11, 5, 1, 11, 3, 5 ] }
var arrList = [1, 9, 0, 2, 1, 6, 8, 11, 0, 5, 18, 1, 26, 11, 8, 2, 3, 5, 18] var evenRepeat = []; var oddRepeat = []; for(var i = 0; i <= arrList.length; i++){ if(arrList[i] % 2 == 0){ oddRepeat.push(arrList[i]) }else{ evenRepeat.push(arrList[i]) } } for (var i = 0; i < oddRepeat.length; i++) { for (var j = 0; j < oddRepeat.length; j++) { if (oddRepeat[i] < oddRepeat[j]) { var t = oddRepeat[i]; oddRepeat[i] = oddRepeat[j]; oddRepeat[j] = t; } if (evenRepeat[i] < evenRepeat[j]) { var k = evenRepeat[i]; evenRepeat[i] = evenRepeat[j]; evenRepeat[j] = k; } } } console.log('list: ',arrList,'\n{') console.log('oddRepeat: ',oddRepeat) console.log('evenRepeat: ',evenRepeat,'\n}')