首页 > 代码库 > JS(一)

JS(一)

循环还是很有意思的:

1) 安全数的作业:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>安全数</title>
</head>
<body>
    <script type="text/javascript">
    for(var i = 1;i<=60;i++)
    {
        if (i%7!=0 && i%10!=7)
         {
            document.write(i+" ");
        }
    }
    </script>
</body>
</html>

2) 0.01毫米的纸对折30次就不得了了:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <script type="text/javascript">
        var j = 0.01;
        for(var i = 1;i<=30;i++)
        {
            j *= 2; 
        }
        document.write(j/1000+"米");
        /*var j = 0.01;
        var i = 0;       
        while (j<=1000) {
            j *= 2;// statement
            i++
        }
        document.write(i);*/
    </script>
</body>
</html>

3) 7个数中的最大值和最小值以及对应的下标:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>哈哈哈哈哈哈</title>
</head>
<body>
    <script type="text/javascript">
    var arr = [10,20,35,87,2,14,7];
    var max = arr[0];
    var min = arr[0];
    var maxIndex,minIndex;
    for(var i = 0; i < 7 ; i++){
        if(max < arr[i])
        {
            max = arr[i];
            maxIndex = i;
        }
        if (min > arr[i])
        {
            min = arr[i];
            minIndex = i;
        }    
    }
    document.write(max+ +maxIndex+"<br>"+min+" "+minIndex);
    </script>
</body>
</html>

 

JS(一)