一、for循环打印杨辉三角
<body>
<style>
span {
color: white;
}
</style>
<script>
function hd(row) {
for (let i = 1; i < row; i++) {
for (let n = row - i; n > 0; n--) { //打印空白row-1,row-2...1
document.write(`<span>^</span>`);
}
for (let m = i * 2 - 1; m > 0; m--) { //打印星号1,3,5...(row-1)*2-1
document.write("*");
}
document.write("<br/>");
}
}
hd(10);
</script>
</body>
二、for-in与for-of使用
1.for-in
取键名遍历:key指对象的键名/数组的下标
<body>
<script>
let yooo = ['alison', 'ruby', 'andy'];
for (let key in yooo) {
console.log(yooo[key]);
}
for (let key in window) {
console.log(window[key]);
}
</script>
</body>
2.for-of
取值遍历:主要处理迭代对象
<body>
<script>
let yooo = ['alison', 'ruby', 'andy'];
for (const value of yooo) {
console.log(value);
}
</script>
</body>
字符串也有迭代特性
<body>
<script>
for (let v of "alison") {
console.log(v);
}
</script>
</body>