//去重1
var arr = [1, 2, 3, 4, 5, 5, 4, 6, 78, 1, 789, 8975, 78]; var ar = []; for(var i in arr) { if(ar.indexOf(arr[i]) == -1) { ar.push(arr[i]); } } document.write("去重:" + ar + "
");
document.write("------------------------------" + "
")
//去重2
function qucong(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(j, 1); //j--; } } } return arr; }
document.write("去重:" + qucong([2, 2, 2, 3, 1]) + "
"); document.write("------------------------------" + "
")
//排序 var arr = [3, 2, 4, 7, 4, 8, 2, 1];
function fn(a, b) { return a - b; }
var aa = arr.sort() document.write("排序:" + aa.join("<") + "
")
document.write("------------------------------" + "
")
//数组排序
var arr = [3, 31, 11, 6, 9, -11]; for(var j = 0; j < arr.length - 1; j++) { //外层循环排5次
for(var i = 0; i < arr.length; i++) {//每次排序得到最后一个值
if(arr[i] >= arr[i + 1]) { //判断相邻两数的大小
var tempArr = arr[i + 1]; //做一个临时变量=第二个数
arr[i + 1] = arr[i]; //将第一个数换到第二个位置
arr[i] = tempArr; //再将装到临时变量的第二个数放到第一个位置
} } }
document.write("数组排序:" + arr + "
"); document.write("------------------------------" + "
")
//去重加排序
var arr = [1, 2, 3, 4, 5, 5, 4, 6, 78, 1, 789, 8975, 78];
var ar = []; for(var i in arr) { if(ar.indexOf(arr[i]) == -1) { ar.push(arr[i]); } }
document.write("去重加排序:" + ar.sort(fn).join("<") + "
")
function fn(a, b) { return a - b; } document.write("------------------------------" + "
")
//阶乘
function jc(num) { if(num == 1) { return 1; } else { return num * jc(num - 1); } }
document.write("阶乘:" + jc(5) + "
");
document.write("------------------------------" + "
")
//水仙花
function sxh() { for(var i = 100; i < 1000; i++) { var bai = parseInt(i / 100); var shi = parseInt(i % 100 / 10);
var ge = parseInt(i % 100 % 10); if(i == bai * bai * bai + shi * shi * shi + ge * ge * ge) { document.write("水仙花:" + i + "
");
} } }
sxh(); document.write("------------------------------" + "
")
//各个位置相加
function kk(num) { var bai = parseInt(num / 100);
var shi = parseInt(num % 100 / 10);
var ge = parseInt(num % 100 % 10);
document.write(bai + shi + ge); } kk(135); document.write("
" + "------------------------------" + "
")
//100以内能够被3整除的所有数 按一行6个进行排列
function hu() { var index = 0;
for(var k = 1; k <= 100; k++) { if(k % 3 == 0) { document.write(k + " "); index++; }
if(index == 6) { document.write("
"); index = 0;
} } }
hu();
document.write("
" + "------------------------------" + "
") //单词首字母大写 var arr = "welcome to rong er"; function szmdx(s) { var i, ss = s.toLowerCase().split(/\s+/); for(var i = 0; i < ss.length; i++) { ss[i] = ss[i].slice(0, 1).toUpperCase() + ss[i].slice(1); } return ss.join(' '); } document.write(szmdx(arr));