javascript入门笔记(2)——判断、循环和函数


judgement


  1. if else

  2. switch(){
    case 1:
    alert();
    break;//if no break, goes to case 2;
    case 2:
    alert();
    break;
    default:
    alert();
    }
  3. <exp>?<v1>:<v2>

    eg:
    max = (a>b)?a:b

loop


while(){
}

var a;
var b;
temp=b;
while(b!=0){
  temp=a%b;
  a=b;
  b=temp;
}
alert("greatest common divisor is"+a);

do{
} while();

//reverse
number= 12334;
reverse=0;
do{
    lastDigit=number%10;
    reverse=reverse*10+lastDigit;
    number=number/10;
}while(number!=0);
alert("the reverse is"+reverse);

for( init; ; ){

}

if for( ; ; ), infinite loop;

break & continue

break: out of the loop;
continue: next of the loop;

function


function name(){
}

function variable

var f = new Function("x","y","return x*y");

**equals to**
function f(x,y){
return x*y}

eg:

function add(a,b){
return a+b;
}

function cal(f,a,b){
return f(a.b);
}
document.write(cal(add,5,6));
  • local variable :
    inside the function
  • global variable
    unlike C, there’s no other kinds of variables,
    i,e, you can’t use nested variables with the same name in a function.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值