41 嵌套的for循环
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset = "utf-8">
<script type="text/javascript">
for(var i = 0; i < 5; i++){
for(var j = 0; j < 5; j++){
document.write("* ");
}
document.write("<br>");
}
for(var i = 1; i <= 5; i++){
for(var j = 1; j <= i; j++){
document.write("* ");
}
document.write("<br>");
}
for(var i = 5; i >= 1; i--){
for(var j = 1; j <= i; j++){
document.write("* ");
}
document.write("<br>");
}
</script>
<style>
</style>
</head>
<body>
</body>
</html>
42 for循环嵌套练习(1)
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset = "utf-8">
<script type="text/javascript">
for(var i = 1; i <= 9; i++){
for(var j = 1; j <= i; j++){
document.write("<span>" + j + "*" + i + "=" + (j * i) + "</span>" );
}
document.write("<br>");
}
</script>
<style>
body {
width: 2000px;
}
span {
display: inline-block;
width: 80px;
}
</style>
</head>
<body>
</body>
</html>
43 for循环嵌套练习(2)
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset = "utf-8">
<script type="text/javascript">
for(var i = 2; i <= 100; i++){
var flag = 1;
for(var j = 2; j <= parseInt(i ** 0.5); j++){
if(i % j == 0){
flag = 0;
break;
}
}
if(flag){
document.write(i + " ");
}
}
</script>
<style>
</style>
</head>
<body>
</body>
</html>
44 break和continue
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset = "utf-8">
<script type="text/javascript">
for(var i = 0; i < 5; i++){
console.log(i);
break;
}
if(true){
break;
console.log(i);
}
outer:
for(var i = 0; i < 5; i++){
console.log("外层循环");
for(var j = 0; j < 5; j++){
break outer;
console.log("内层循环");
}
}
for(var i = 0; i < 5; i++){
console.log(i);
if(i == 2){
continue;
}
}
console.time("test");
for(var i = 0; i < 1000000; i++){
for(var j = 0; j < i; j++){
}
}
console.timeEnd("test");
</script>
<style>
</style>
</head>
<body>
</body>
</html>
45 质数练习补充
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset = "utf-8">
<script type="text/javascript">
for(var i = 2; i <= 100; i++){
var flag = 1;
for(var j = 2; j <= Math.sqrt(i); j++){
if(i % j == 0){
flag = 0;
break;
}
}
if(flag){
document.write(i + " ");
}
}
var result = Math,sqrt(4);
</script>
<style>
</style>
</head>
<body>
</body>
</html>