Blogger Information
Blog 12
fans 0
comment 0
visits 5387
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
实例演示分支与循环结构
汉邦
Original
432 people have browsed it

单分支

  1. let status = true
  2. if (status) {
  3. console.log('Success')
  4. }

双分支

  1. status = false
  2. if (status) {
  3. console.log('Success')
  4. } else {
  5. console.log('Fail')
  6. }

多分支

  1. let age = 7
  2. if (age >= 18 && age < 60) {
  3. console.log('请放心观看')
  4. } else if (age >= 60) {
  5. console.log('请在子女的陪同下观看')
  6. } else {
  7. console.log('未成年,禁止观看')
  8. }

循环结构

  • 三要素:
    — 初始化: 从哪里开始,入口
    — 条件: 到哪里结束,true继续,false停止
    — 更新条件: 避免死循环
  1. while

    1. let arr = ['red', 'green', 'blue']
    2. console.log(arr)
    3. let i = 0
    4. console.log(arr.length)
    5. while (i < arr.length) {
    6. console.log(arr[i])
    7. i++
    8. }
  2. for,while的简化

    1. for (let i = 0; i < arr.length;i++) {
    2. console.log(arr[i])
    3. }
  3. for-of: 遍历”数组”,只关注值

    1. for (let value of arr) {
    2. console.log(value)
    3. }
  4. for-in: 遍历”对象”

    1. const user = { id:5, myname:'jay', salary:6000 }
    2. for (let key in user){
    3. console.log(user[key])
    4. }
  5. forEach: 遍历”数组”

    1. arr.forEach(function (item) {
    2. console.log(item)
    3. })
  6. map: 遍历数组,与 forEach 使用方法一样,只不过有一个返回值

    1. arr = [1, 2, 3]
    2. let res = arr.map(item => item * 2)
    3. console.log(res)
Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post