JS (Coursera): For Loops
2016-06-06 09:11
344 查看
For Loops
You’ll be able to create different kinds of for loopfor
clearly shows the start and end valuesis especially good for handling a series of data
(data_structure.length tells you how many items data_structure contains)
<html><head> <script> var continents=["Australia", "Africa", "Antarctica", "Eurasia", "America"]; var response, count=0; for (var index=0; index < continents.length; index++) { response = confirm("Have you been to " + continents[index] + "?"); if (response) count++; } alert("You have been to " + count + " continents!"); </script> </head></html>
for…in
gives you the index of each item<!doctype html> <html><head> <script> var continents=["Australia", "Africa", "Antarctica", "Eurasia", "America"]; var response, count=0; for (var index in continents) { response=confirm("Have you been to " + continents[index] + "?"); if (response) count++; } alert("You have been to " + count + " continents!"); </script> </head></html>
this example shows how for…in can be used to access the content of a data structure(like json).
<!doctype html> <html><head> <title>Example of for in</title> <script> var response, count=0; var onePerson = { initials:"DR", age:40,job:"Professor" }; for (var property in onePerson) { alert(property + "=" + onePerson[property]); } </script> </head></html>
for…of
gives you each item(not index)<!doctype html> <html><head> <title>Example of for of</title> <script> var continents=["Australia", "Africa", "Antarctica", "Eurasia", "America"]; var response, count=0; for (var continent of continents) { response = confirm("Have you been to " + continent + "?"); if (response) count++; } alert("You have been to " + count + " continents!"); </script> </head></html>
Loop Control
You’ll be able to control loops in two new waysbreak continue
break
breaktotally stops the loop
continue
continuestops the current iteration
相关文章推荐
- JQuery1——基础($对象,选择器,对象转换)
- Android学习笔记(二九):嵌入浏览器
- Android java 与 javascript互访(相互调用)的方法例子
- JavaScript演示排序算法
- javascript实现10进制转为N进制数
- 最后一次说说闭包
- Ajax
- 2019年开发人员应该学习的8个JavaScript框架
- HTML中的script标签研究
- 对一个分号引发的错误研究
- 异步流程控制:7 行代码学会 co 模块
- ES6 走马观花(ECMAScript2015 新特性)
- JavaScript拆分字符串时产生空字符的原因
- Canvas 在高清屏下绘制图片变模糊的解决方法
- Redux系列02:一个炒鸡简单的react+redux例子
- JavaScript 各种遍历方式详解
- call/apply/bind 的理解与实例分享