function writeDocument(variable){
document.write(variable+"<br/>");
}
//数组创建
var arr = ["HTML","CSS","JavaScript","jQuery","Vue.js"];
writeDocument(arr);
//数组下标取值
writeDocument(arr[2]);
//数组赋值
arr[4]="React.js";
writeDocument(arr);
arr[5]="Vue.js";
writeDocument(arr);
arr[8]="Angular.js";
writeDocument(arr);
writeDocument("获取数组长度--------------------");
writeDocument("数组长度是:"+arr.length);
writeDocument("获取数组某一段--------------------");
writeDocument("数组1-2号元素是:"+arr.slice(1,3));
writeDocument("往数组头添加元素--------------------");
writeDocument(arr.unshift("Web","Technology"));
writeDocument(arr);
writeDocument("往数组尾添加元素--------------------");
writeDocument(arr.push("Bootstrap","Sass"));
writeDocument(arr);
writeDocument("删除数组的第一个元素--------------------");
writeDocument("删除元素:"+arr.shift());
writeDocument(arr);
writeDocument("删除数组的最后一个元素--------------------");
writeDocument("删除元素:"+arr.pop());
writeDocument(arr);
writeDocument("对字符串数组按升序排序--------------------");
writeDocument(arr.sort());
var numArr=[3,6,9,1,4,11];
writeDocument("排序前:"+numArr);
function downcompare(l,r){
if(l>r){
return -1;
}
if(l==r){
return 0;
}
if(l<r){
return 1;
}
}
numArr.sort(downcompare);
writeDocument("降序排序后:"+numArr);
function upcompare(l,r){
if(l>r){
return 1;
}
if(l==r){
return 0;
}
if(l<r){
return -1;
}
}
numArr.sort(upcompare);
writeDocument("升序排序后:"+numArr);
writeDocument("反转数组:"+numArr.reverse);
writeDocument("默认Join数组:"+numArr.join());
writeDocument("默认Join数组:"+numArr.join(":"));
writeDocument("默认Join数组:"+numArr.join("-"));
writeDocument("默认Join数组:"+numArr.join("><"));
function addAngleBracket(strValue){
var strArr =strValue.split("");
console.log(strArr);
var str = strArr.join("><");
console.log(str);
strArr=str.split("");
console.log(strArr);
strArr.unshift("<");
strArr.push(">");
console.log(strArr);
return strArr.join("");
}
writeDocument(addAngleBracket("绿叶学习网"));
function getSize(width, height, depth)
{
var area = width * height;
var volume = width * height * depth;
var sizes = [area, volume];
return sizes;
}
var arr = getSize(30, 40, 10);
writeDocument("底部面积为:" + arr[0] + "<br/>");
writeDocument("体积为:" + arr[1]);
console