<!DOCTYPE HTML>
<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>数组</title>
<script type="text/javascript">
//创建数组
var arr=['*','**','***','****']
//显示数组长度
document.write(arr.length);
//将数组内容输出,完成达到的效果。
document.write(`<br/>`);
document.write(arr[0]);
document.write(`<br/>`);
document.write(arr[1]);
document.write(`<br/>`);
document.write(arr[2]);
document.write(`<br/>`);
document.write(arr[3]);
</script>
</head>
<body>
</body>
</html>
<!DOCTYPE HTML><html ><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>数组</title><script type="text/javascript"> //创建数组 var arr = ['*','##',"***","&&","****","##*"]; arr[7] = "**"; //显示数组长度 alert(arr.length); //将数组内容输出,完成达到的效果。 var arr2 = arr.filter(item => item >= '*') arr2.sort() arr2.forEach((v,i) => { document.write(`${arr2[i]}<br/>`); });</script></head><body></body></html>
更简洁的代码:
<!DOCTYPE HTML>
<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>数组</title>
<script type="text/javascript">
//创建数组
var arr=['@','$$','**','%%'];
//显示数组长度
var len=arr.length;
alert(len);
//将数组内容输出,完成达到的效果。
for(var i=0;i<len;i++){
document.write(arr[i]+'<br>');
}
</script>
</head>
<body>
</body>
</html>
所以为什么不用循环呢??
题目要求用alert,输出数组是var arr = ['*','##',"***","&&","****","##*"];,虽然不验证答案也无所谓