<script>
/**场景1
*计算对象数组中每个电脑的操作系统是否可用
*大于16位操作系统表示可用,否则不可用
*/
var computers =[
{name:'apple',ram:16},
{name:'ibm',ram:4},
{name:'acer',ram:32}
]
var everComputersCanRunProgram = true;
var someComputersCanRunProgram = false;
for(let i=0;i<computers.length;i++){
var computer=computers[i]
if(computer.ram<16){
everComputersCanRunProgram = false;
}
else{
someComputersCanRunProgram = true;
}
}
console.log(everComputersCanRunProgram)
console.log(someComputersCanRunProgram)
// every 其中有一个为假即假(false)
// some 其中有一个为真即真(true)
var every = computers.every((computer)=>{
return computer.ram<16
})
console.log("============")
console.log(every)
var some = computers.some((computer)=>{
return computer.ram<16
})
console.log("============")
console.log(some)
/**场景2
*假定有一个注册页面,判断所有input内容的长度是否大于0
*/
console.log("============")
function Filed(value){
this.value = value
}
Filed.prototype.validate = function(value){
return this.value.length>0
}
var username = new Filed("xiaotan")
var password = new Filed("12345")
console.log(username.validate())
console.log(password.validate())
console.log("============")
var fields = [username,password]
var formIsValid = fields.every((field)=>{
return field.validate();
})
console.log(formIsValid)
if(formIsValid){
//注册成功
}else{
//请填写完整信息
}
</script>
数组方法every,some
最新推荐文章于 2024-05-15 14:13:03 发布