1. 程式人生 > >js 循環

js 循環

ont 定義變量 console art i++ leg 利用 del AR

練習

利用for循環計算1 * 2 * 3 * ... * 10的結果 實現思路:

1.定義起始變量 var x=1(因為是乘運算,所有定義變量x=1,如果是加法var x=0) var i 2.for循環

var x = 1;
var i;
for(i=1;i<=10;i++){

x=x*i;

}

if (x === 3628800) {

    console.log(‘1 x 2 x 3 x ... x 10 = ‘ + x);
}
else {
    console.log(‘計算錯誤‘);
}
請利用循環遍歷數組中的每個名字,並顯示Hello, xxx! 實現思路
1.定義一組數組 for循環遍歷
var arr = [‘Bart‘, ‘Lisa‘, ‘Adam‘];
for(i=0;i < arr.legth;i++){
console.log(‘hello,‘+arr[i]);
}


js 循環