var attr = [12, 34, 2, 5, 76]; var sum = 0; for(var i = 0; i < attr.length; i++) { sum = sum + attr[i]; } alert(sum);
var arr = [4, 0, 7, 9, 0, 0, 2, 6, 0, 3, 1, 0]; var a = []; for(i = 0; i < arr.length; i++) { if(arr[i] == 0) { continue; } a.push(arr[i]); } document.write(a);
var attr = [2, 34, 2, 5, 6, 9, 8, 6, 4, 34, 67, 6]; var b = []; for(i = 0; i < attr.length; i++) { for(j = 0; j < attr.length; j++) { for(k = 0; k < attr.length - 1 - j; k++) { if(attr[k] > attr[k + 1]) { var temp = attr[k]; attr[k] = attr[k + 1]; attr[k + 1] = temp; } } } if(attr[i] != attr[i + 1]) { b.push(attr[i]); } } alert(b);
var attr = [12, 34, 2, 5, 76]; var a = attr[0]; for(i = 0; i < attr.length; i++) { if(attr[i] > a) { a = attr[i]; } } alert(a);
var a = new Array(); var b = []; var bs = 0; var i = -1; while(true) { i++; a[i] = parseInt(Math.random() * 90 + 10); if(b.indexOf(a[i]) == -1) { b.push(a[i]); bs++; if(bs == 10) { break; } } } for(j = 0; j < b.length; j++) { for(k = 0; k < b.length - 1 - j; k++) { if(b[k] > b[k + 1]) { var temp = b[k]; b[k] = b[k + 1]; b[k + 1] = temp; } } } document.write(b);
原文:https://www.cnblogs.com/cgj1994/p/9520196.html