console
var arr=[1,2,3,4,5,6,7,8];
document.write("数组为:"+arr+'</br>');
document.write("--------------获取数组的长度--------------"+'</br>');
document.write("数组的长度为:"+arr.length+'</br>');
document.write("--------------获取数组的1-2号元素--------------"+'</br>');
document.write(arr.slice(0,2)+'</br>');
document.write("--------------往数组头添加元素--------------"+'</br>');
arr.unshift(0);
document.write("数组为:"+arr+'</br>');
document.write("--------------往数组尾部添加元素--------------"+'</br>');
arr.push(9);
document.write("数组为:"+arr+'</br>');
document.write("--------------删除数组的第一个元素--------------"+'</br>');
arr.shift()
document.write("数组为:"+arr+'</br>');
document.write("--------------删除数组的最后一个元素--------------"+'</br>');
arr.pop()
document.write("数组为:"+arr+'</br>');
document.write("--------------对数组中的元素进行升序的排列--------------"+'</br>');
document.write(arr.sort()+'</br>');//均会转换为字符串
var numarr=[3,5,2,6,9,4];//升序排列函数
function upcompare(left,right)
{
if(left-right>0)
{
return 1;
}
if(left-right==0)
{
return 0;
}
if(left-right<0)
{
return -1;
}
}
//document.write(numarr.sort(upcompare)+'</br>');
document.write("--------------反转数字数组--------------"+'</br>');
document.write("原数组为:"+numarr+'</br>');
document.write("反转后,数组为:"+numarr.reverse()+'</br>');
document.write(numarr.join(":")+'</br>');
document.write("--------------给字符添加尖括号--------------"+'</br>');
function addanglebracket(strvalue)
{
var temp=strvalue.split("").join("><");
var temp1=temp.split("");
temp1.unshift("<");
temp1.push(">");
return temp1.join("");
}
var strvalue="绿叶学习网";
document.write(strvalue+'</br>');
document.write(addanglebracket(strvalue)+'</br>');
document.write("--------------javascript时间对象--------------"+'</br>');
document.write("--------------javascript时间对象--------------"+'</br>');
function write(variable)
{
document.write(variable+"</br>");
}
write("---------------定义时间对象-------------------");
var oToday =new Date();
write("-------------------获取年,月,日,时,分,秒,毫秒-----------------");
write("年:"+oToday.getFullYear());
write("月:"+oToday.getMonth());
write("日:"+oToday.getDate());
write("时:"+oToday.getHours());
write("分:"+oToday.getMinutes());
write("秒:"+oToday.getSeconds());
write("毫秒:"+oToday.getMilliseconds());
write("-------------------获取年,月,日,时,分,秒,毫秒-----------------");
var oDate= new Date(2019);
oDate.setFullYear(2020);
write("年:"+oDate.getFullYear());
oDate.setMonth(5);
write("月:"+(oDate.getMonth()+1));
oDate.setDate(1);
write("日:"+oDate.getDate());
oDate.setHours(16);
write("时:"+oDate.getHours());
oDate.setMinutes(58);
write("分:"+oDate.getMinutes());
oDate.setSeconds(59);
write("秒:"+oDate.getSeconds());
oDate.setMilliseconds(999);
write("毫秒:"+oDate.getMilliseconds());
var week=["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];
write("----------打印2019年6月1日 星期六 15:59:59:999-------------");
var oPrintDate = new Date(2019, 5, 1, 15, 59, 59, 999);
write(oPrintDate.getFullYear()+"年"+(oPrintDate.getMonth()+1)+"月"+oPrintDate.getDate()+"日"+week[oPrintDate.getDay()]+oPrintDate.getHours()+":"+oPrintDate.getMinutes()+":"+oPrintDate.getSeconds()+":"+oPrintDate.getMilliseconds());
document.write("--------------javascript数学对象--------------"+'</br>');
document.write("--------------javascript数学对象--------------"+'</br>');
write("----------生成一个随机数-------------");
function generateVerifyCode(length)
{
var strvalue="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var code="";
for (var i=0;i<length;i++)
{
var randomIndex =Math.floor(Math.random()*strvalue.length);
code +=strvalue.charAt(randomIndex);
}
return code;
}
write(generateVerifyCode(4));
write(generateVerifyCode(8));
write("--------------用Math的方法找出3,9,12,99,45,78中的最大值和最小值-------------")
write("其中的最大值为:"+Math.max(3,9,12,99,45,78));
write("其中的最小值为:"+Math.min(3,9,12,99,45,78));
write("--------------请随机生成一个颜色值rgb(X,Y,Z)的函数,其中X,Y,Z表示的是0~255的数字-------------")
function randomcolor ()
{
var r;
var g;
var b;
var color=[r, g, b];
for(var i=0;i<3;i++)
{
color[i]=Math.floor(Math.random()*(255+1));
}
return color;
}
write(randomcolor ());
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script>
</script>
</head>
<body>
</body>
</html>