<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<h1>请输入英文下的逗号、数字</h1>
<input type="text" placeholder="请输入数字逗号相隔的内容" id="txta">
<button id="btn">点击查看结果</button>
<input type="text" id="txtb">
</body>
<script>
btn.onclick=function(){
let value = txta.value;
let newArr = value.split("");
console.log(newArr);
let endArr = [];
for(var i = 0; i < newArr.length; i++ ){
// debugger
console.log(newArr[newArr.length-1])
if(newArr[i] !== ',' && typeof newArr[i] ==="string"){
endArr.push(Number(newArr[i]))
}
}
quchongPaixu(endArr);
txtb.value = endArr;
}
//去重排序
function quchongPaixu(arr){
for(var i = 0; i < arr.length; i++){
for(var j = i + 1; j <arr.length; j++){
if(arr[i] === arr[j]){
arr.splice(i,1);
i--;
}
}
}
for (var i = 0; i < arr.length-1; i++){
for (var j = i + 1; j < arr.length; j++) {
if (arr[i] > arr[j]) {
//交换
var temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
};
</script>
</html>
网友评论