1. 程式人生 > >JavaScript基礎

JavaScript基礎

number amp 數字 creat fin 日期 eth parse argument

/*
Function:nmn
Description:打印九九乘法表
Author:Nick.Chung
Create:2017-06-19
Parameter:None
Return:None
*/
var nmn = function () {
for (var i = 1; i <= 9; i++) {
for (var j = 1; j <= i; j++) {
document.write(
i.toString() + "x" + j.toString() //輸出ixj字符
+ "=" + i * j //輸出i*j乘積
+ "&nbsp;"); //輸出空格
}

document.write("<br>"); //輸出換行符(每行)
}
}

//以表格形式打印九九乘法表
var nmnWithTable = function () {
document.write("<table>")
for (var i = 1; i <= 9; i++) {
document.write("<tr>");
for (var j = 1; j <= i; j++) {
document.write(
"<td style=‘border:1px solid gray;‘>"
+ i.toString() + "x" + j.toString() //輸出ixj字符
+ "=" + i * j //輸出i*j乘積
+ "&nbsp;") //輸出空格
+ "</td>";
}

document.write("</tr>");
}
document.write("</table>");
}

//求算術
var square = function (type) {
var input = prompt("請輸入數字");
//如果是數字
if (input.length > 0 && !isNaN(input)) {
var num = parseInt(input);
switch (type) {
case "2T":
alert(Math.pow(num, 2));
break;

case "3T":
alert(Math.pow(num, 3));
break;

case "SQRT":
alert(Math.sqrt(num, 2));
break;

case "ROUND":
alert(Math.round(num));
break;

default:
break;
}
}
else {
alert("不是數字!請重新輸入");
}

}

//畫等腰三角型
var writeHR = function () {
for (var i = 1; i <= 30; i++) {
document.write("<hr style=‘width:" + i * 10 + "px‘ />");
}
}

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript" src="Scripts/common.js"></script>
</head>
<body>
    <script type="text/javascript">
        document.writeln(typeof (1) + ‘<br>‘);
        document.writeln(typeof (NaN) + ‘<br>‘);
        document.writeln(typeof (Number.MIN_VALUE) + ‘<br>‘);
        document.writeln(typeof (Infinity) + ‘<br>‘);
        document.writeln(typeof ("123") + ‘<br>‘);
        document.writeln(typeof (true) + ‘<br>‘);
        document.writeln(typeof (window) + ‘<br>‘);
        document.writeln(typeof (document) + ‘<br>‘);
        document.writeln(typeof (null) + ‘<br>‘);
        document.writeln(typeof (eval) + ‘<br>‘);
        document.writeln(typeof (Date) + ‘<br>‘);
        document.writeln(typeof (sss) + ‘<br>‘);
        document.writeln(typeof (undefined) + ‘<br>‘);

        var arr = ["A", "B", "C", "D", "E"];
        var msg = "";
        for (var i = 0; i < arr.length; i++) {
            msg += arr[i] + ‘<br>‘;
        }

        var joinMsg = arr.join(‘,‘);

        document.write(msg);
        document.write(joinMsg + "<br>");

        var article = ["我在南方學院讀書!", "我的興趣是什麽?", "我的夢想是什麽?"]
        var result = article.join(‘<br>‘)
        document.write(result + "<br>");

        function doSomething() {
            var newMsg = "";
            for (i = 0; i < 10; i++) {
                if (i == 3) continue;
                newMsg = newMsg + "The number is " + i + "<br>";
            }

            return newMsg;
        }

        function howManyArgs() {
            document.write(arguments.length + "<br>");
        }

        howManyArgs("string", 45);
        howManyArgs();
        howManyArgs(12);

        function f1() {
            var n = 999;
            function f2() {
                document.write(n + "<br>");
                n++;
            }
            return f2;
        }
        var result = f1();
        result(); // 999
        result(); // 1000

        nmn();
        nmnWithTable();

        square(‘ROUND‘);

        writeHR();
    </script>

    <script type="text/javascript">
        var timeAlert = function () {
            var dt = new Date();
            var dtStr = dt.toLocaleString();
            var hour = dt.getHours();
            var msg = "";

            if (hour >= 6 && hour < 9) {
                msg = "早上好";

            }
            else if (hour >= 9 && hour <= 12) {
                msg = "上午好";
            }
            else if (hour > 12 && hour <= 18) {
                msg = "下午好";
            }
            else {
                msg = "晚上好";
            }


            alert(dtStr + "," + msg);

        }

        timeAlert();
</script>

<table border=1>
<tr name=‘th‘><td>折扣價</td><td>出發日期</td><td>出發地</td><td>目標地</td><td>折扣</td></tr>
<tr name=‘tr‘><td>$267</td><td>2015-06-05</td><td>北京</td><td>南京</td><td>1.6折</td></tr>
<tr name=‘tr‘><td>$267</td><td>2015-06-05</td><td>北京</td><td>南京</td><td>1.6折</td></tr>
<tr name=‘tr‘><td>$267</td><td>2015-06-05</td><td>北京</td><td>南京</td><td>1.6折</td></tr>
<tr name=‘tr‘><td>$267</td><td>2015-06-05</td><td>北京</td><td>南京</td><td>1.6折</td></tr>
<tr name=‘tr‘><td>$267</td><td>2015-06-05</td><td>北京</td><td>南京</td><td>1.6折</td></tr>
<tr name=‘tr‘><td>$267</td><td>2015-06-05</td><td>北京</td><td>南京</td><td>1.6折</td></tr>
</table>

<script type="text/javascript">
    var trArr = document.getElementsByName(‘tr‘);
    for (var i = 0; i < trArr.length; i++) {
        if (i % 2 == 0) {
            trArr[i].style.backgroundColor = ‘red‘;
        }
        else {
            trArr[i].style.backgroundColor = ‘blue‘;
        }
    }
</script>
</body>
</html>

  

JavaScript基礎