'use strict';
//二,7,循环
//二,7,循环
//计算1到10000之和
var x = 0;
var i;
//(初始条件;判断条件;递增条件)
for(i=1; i<=10000; i++){
x = x + i;
}
console.log(x);
//练习:计算1*2*3*...*10的结果:
var x = 1;
var i;
for(i=1; i<=10; i++){
x *= i;
}
console.log(x);
if (x === 3628800){
console.log('1x2x3x...x10='+x);
}else{
console.log('计算错误');
}
//for循环最常用的地方是利用索引来遍历数组
var arr = ['Apple','Google','Microsoft'];
var i ,x;
for(i=0; i<arr.length; i++){
x = arr[i];
console.log(x);
}
//break退出循环
var a = 0;
for(a=1;a<=20;a++){
console.log(a);
break;
}
//for...in可以把一个对象的所有属性依次循环起来
var fi = {
name: 'Jack',
age: 20,
city: 'Beijing'
};
for(var key in fi){
console.log(key);
}
//hasWonProperty()过滤掉对象继承的属性
var o = {
name: 'Jack',
age: 20,
city: 'Beijing'
};
for(var key in o){
if(o.hasOwnProperty(key)){
console.log(key);
}
}
//for...in循环出对象的索引
var a = ['A','B','C'];
for(var i in a){
console.log(i);
console.log(a[i]);
}
//请注意,for...in对Array的循环得到的是String而不是Number
//while
var x = 0;
var n = 99;
console