<香港科技大学html+css+js课堂笔记>week2--javaScript第二部分

6.选择语句:

if...else if...else...

switch...case...default...

EG1:

<!doctype html>
<html><head><script>
  var user_name;
  user_name=prompt("What is your name?");
  if (user_name == "dave")
    alert("Great name!");
  else if (user_name == "jogesh")
    alert("Pretty good name!");
  else if (user_name == "oz")
    alert("Excellent name!");
  else
    alert("Your name isn't great, never mind...");
</script></head></html>

EG2:

<!doctype html>
<html>
    <head>
        <script>
        var user_name=prompt("What is your name?");
        switch(user_name) {
            case "dave":
                alert("Great name!"); 
                break;
            case "jogesh":
                alert("Pretty good name!"); 
                break;
            default:
                alert("Your name isn't great, never mind...");
        }
        </script>
    </head>
</html>


7.循环语句:

while...

do...while(condition);


var text = "The cat's hat was wet";
result = text.indexOf("at");

Eg1:

<!doctype html>
<html><head>
    <title>Example of while()</title>
    <script>
        var response, finished;
        finished=false;
        alert("Rossiter is a great name.");
        while (!finished){
            response=prompt("Do you agree?");  
            if (response.indexOf("y")==0)  
                finished=true; 
        }
    </script>
</head></html>


Eg2:

<!doctype html>
<html><body>
  <script>
    function show_money() {
      alert("In the function, the value is: "+ money);
    }
    var money = 99;
    alert("In the main part, the value is: "+ money);
    show_money();
    alert("In the main part, the value is: "+ money);
  </script>
</body></html>


8.局部变量和全局变量:

全局变量:未声明自动转为全局变量;

Eg:

<!doctype html>
<html><body>
  <script>
    function show_money() {
      money = 2; //money全局变量
      alert("In the function, the value is: "+ money);
    }
    show_money();
    alert("In the main part, the value is: "+ money);
  </script>
</body></html>


9.布尔值与逻辑运算符:

True、False     ||、 &&、!

Eg:

<html><body><script>
  var you_are_rich = false;
  var you_have_partner = true;
  var you_have_flat = true;
  var life_is_fantastic = you_are_rich && you_have_partner && you_have_flat;
  alert("life is fantastic is " +  life_is_fantastic);
  you_are_rich = true;
  life_is_fantastic = you_are_rich && you_have_partner && you_have_flat;
  alert("life is fantastic is now " +  life_is_fantastic);
</script></body></html>


10.数组:

var pets = ["Dog", "Cat", "Rabbit"];

var pets = new Array(10);

数组相关函数:

1》join(separator):Use array.join(separator) to convert array into string.

separator 默认为‘,’

Eg:

var pets = ["Dog", "Cat", "Rabbit"];
alert(pets.join(" and "));
// This shows "Dog and Cat and Rabbit"

2>alert(pets[2]); pets[2]="Pig";

3>pets.length;

4>pets.push("Pig");添加“pig”到结尾;索引自动更新;

5>pets.unshift("pig");在数组开头添加“pig”;

6>pets.pop(); 从数组中删除结尾元素,返回值为删除元素;

7>pets.shift();从数组开头删除元素,返回值为开头元素。

8>pets.concat(array2);合并两个数组

Eg:

var pets = ["Dog", "Cat", "Rabbit", "Hamster"];
var primes = [2, 3, 5, 7, 11];
var result = pets.concat(primes);
// result is ["Dog", "Cat", "Rabbit", "Hamster",
//            2, 3, 5, 7, 11]


11.

1>随机数的产生:

var random_number = Math.random();

2>得到随机数范围:

random_number = Math.random() * max_value; [0, max_value)

3>截取小数的小数部分(不四舍五入):

Math.floor(2.154386) = 2;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值