美文网首页
JS循环语句--篇7

JS循环语句--篇7

作者: 扶得一人醉如苏沐晨 | 来源:发表于2023-10-13 08:56 被阅读0次

    循环的概念

    重复的执行一段的代码,避免死循环,提高效率(时间复杂度(主要关注) 空间复杂度(不关注))

    循环包含三大语句:while语句、do while语句、for语句。

    循环的三要素

    • 初始值(初始的变量值)
    • 条件(基于初始值的判断)
    • 迭代量(基于初始值的变化)

    一、while语句

    优点:

    • 只要条件为 true,就可以一直重复执行循环体,使用灵活;
    • 代码简单,容易理解。

    缺点:

    • 如果循环条件一直为 true,会导致死循环,可能会造成性能问题;
    • 循环计数器需要在外部定义,增加了代码量。
    while(条件表达式(返回true和false)){
      执行的代码
    }
    let 初始值变量 = 值
    while(条件){
      迭代量
      执行的代码
    }
    

    eg:(循环打印1~10)

    let i = 1
    while(i<=10){
        console.log(i)
        i++
    }
    

    eg:(1+2+...+100)

    // 1加到100
    let number = 0;
    let sum = 0;
    while (number < 100) {
      number++;
      sum += number;
    }
    console.log(sum);
    //100加到1
    let i = 100;
    sum = 0;
    while (i >= 1) {
      sum += i;
      i--;
    }
    console.log(sum);
    

    二、do while

    优点:

    • 至少会执行一次循环体;
    • 只要条件为 true,就可以一直重复执行循环体,使用灵活;
      代码简单,容易理解。

    缺点:

    • 如果循环条件一直为 true,会导致死循环,可能会造成性能问题;
    • 循环计数器需要在外部定义,增加了代码量。
    do{
      执行的代码
    }while(条件)
    
    let 初始值 = 值
    do{
        执行的代码
        迭代量变化
    }while(条件)
    

    eg:上厕所

    let isEmpty = true
    do{
        console.log('有人不')
        isEmpty = false
    }while(isEmpty)
    

    eg:1+2+...+100

    let i = 0;
    let sum = 0;
    do {
      i++;
      sum += i;
    } while (i < 100);
    

    while和do while的区别:

    • while是先判断后执行 do while是先执行后判断
    • do while最少执行一次 while最少执行0次
    • 常用while来写一些执行多次的内容,且最少执行次数没规定时 do while(规定必须要执行的时候)

    三、for循环

    优点:

    • 最常用的循环语句之一,使用灵活,适用于大多数循环场景;
    • 可以通过循环计数器来精确控制循环次数,可控性较高;
    • 可以在循环头部定义循环计数器的初始值、循环条件和循环计数器的更新方式。

    缺点:

    • 在循环前需要定义循环计数器,对于不需要计数器的循环场景不太适用。
    for (初始值; 条件; 迭代量) {
      //执行的代码 跟if一样如果你执行的代码只有情况下可以省略{}
    }
    

    eg:1+2+...+100

    let sum = 0;
    for (let i = 1; i <= 100; i++) {
      sum += i;
    }
    

    面试题:for(;;)是错误的吗?(没错!!!也就意味对应的内容可以被省略)

    //死循环 省略迭代量 和 条件 必定是死循环
    for (;;) {
      console.log("hello");
    }
    

    3.1、for…in 循环:

    for...in 循环用于遍历一个对象的所有可枚举属性。例如:

    const person = { firstName: "John", lastName: "Doe", age: 25 };
    for (const key in person) {
      console.log(key + ": " + person[key]);
    }
    

    优点:

    • 可以遍历对象的可枚举属性,方便获取对象属性;
    • 使用简单,代码量少。

    缺点:

    • 不适用于遍历数组等序列结构的数据类型,因为它遍历的是对象的属性;
    • 遍历顺序不确定。

    3.2、for…of 循环:

    优点:

    • 可以遍历可迭代对象的每个元素,包括数组、字符串、Set、Map 、FormData实例等;
    • 代码简单,易于理解。

    缺点:

    • 不适用于遍历对象的属性。

    for...of 循环用于遍历可迭代对象entries中的每个元素。例如:

    const colors = ["red", "green", "blue"];
    for (const color of colors) {
      console.log(color);
    }
    

    可迭代对象FormData实例

    const fd = new FormData();
          fd.append("name", "zs");
          fd.append("age", 18);
          for (const [key, value] of fd) {
            console.log(key + ":" + value);
          }
          // name:zs
          // age:18
    

    四、JS中break、continue、return跳出循环的用法和区别

    等明天文章发布了---在插入链接哈哈哈哈

    &&&&&&&&&&&&&&&&&&&&&&&

    五、JavaScript时间复杂度

    while的时间复杂度低于for循环 (算法的优化可以使用while来替代for)

    等明天文章发布了---在插入链接哈哈哈哈
    &&&&&&&&&&&&&&&&&&&&&&&

    六、循环嵌套(将多个循环嵌套在一起)

    循环嵌套最多套俩层 O(n^2)
    eg:打印一个正方形

    /*
     * * * * * * * * * * 
     * * * * * * * * * * 
     * * * * * * * * * * 
     * * * * * * * * * * 
     */
    //外层控制行 内层循环控制列
    let row = 4;
    let col = 10;
    for (let i = 0; i < row; i++) {
      let j = 0;
      //每行的字符串
      let rowStr = "";
      while (j < col) {
        j++;
        //字符串拼接
        rowStr += "*";
      }
      document.write(rowStr + "<br/>");
    }
    //执行次数 外层循环次数*内层循环次数
    

    eg2

    image.png
    for (let i = 0; i < 5; i++) {
      //内层的循环条件依赖于外层
      let str = "";
      for (let j = 0; j <= i; j++) {
        str += "*";
      }
      document.write(str + "<br/>");
    }
    

    七、关于循环的各种题:

    // 1, 一个新入职,月工资为2000元的员工,每年涨当年工资5%,20年后的月工资是多少?
    let money = 2000;
    for (let num = 0; num < 20; num++) {
      money *= 1 + 0.05;
    }
    money = parseInt(money);
    console.log("20年后的月工资是" + money);
    
    //2, 山上有一口缸可以装50升水,现在有15升水。老和尚叫小和尚下山挑水,每次可以挑5升。问:小和尚要挑几次水才可以把水缸挑满?通过编程解决这个问题。
    // let num = 5
    let cishu = 0;
    for (let start = 0; start < 35; start += 5) {
      cishu++;
    }
    console.log(cishu);
    
    // 3, 打印100–200之间所有能被3或者7整除的数
    for (let a = 100; a <= 200; a++) {
      if (a % 3 == 0 || a % 7 == 0) {
        console.log(a);
      }
    }
    
    // 4, 计算10的阶乘   (1*2*3*4*5*6*7*8*9*10   n的阶乘1*2……*n)
    let sum = 1;
    for (let a = 1; a <= 10; a++) {
      sum *= a;
    }
    console.log(sum);
    
    // 5, 计算1+3+5+...+99的和
    let sum = 0;
    for (a = 1; a <= 100; a++) {
      if (a % 2 != 0) {
        sum += a;
      }
    }
    console.log(sum);
    
    // 6, 99乘法表
    let row = 9;
    let col = 9;
    for (let i = 1; i <= row; i++) {
      sum = 0;
      for (let j = 1; j <= i; j++) {
        sum = i * j;
        // console.log(i+'x'+j+'='+sum)
        document.write(j + "x" + i + "=" + sum + " ");
      }
      document.write("<br/>");
    }
    
    let row = 9;
    let col = 9;
    for (let i = 1; i <= row; i++) {
      // sum = 0
      str = "";
      for (let j = 1; j <= i; j++) {
        // sum = i * j
        // console.log(i+'x'+j+'='+sum)
        // document.write(j+'x'+i+'='+sum+' ')
        str += j + "x" + i + "=" + i * j + " ";
      }
      console.log(str);
      // document.write('<br/>')
    }
    
    // 7, 输出20~80之间能被3整除的整数, 每行5个
    let a = 0;
    for (i = 20; i <= 80; i++) {
      if (i % 3 == 0) {
        document.write(i + " ");
        // console.log(i);
        a++;
      }
    
      if (a % 5 == 0) {
        document.write("<br/>");
      }
    }
    // 打印的方法
    let a = 0;
    let srt = "";
    for (i = 20; i <= 80; i++) {
      if (i % 3 == 0) {
        // document.write(i + ' ')
        // console.log(i);
        srt += i + "\t";
        a++;
      }
      // console.log(srt);
      if (a % 5 == 0) {
        // document.write('<br/>')
        srt += "\n";
      }
    }
    console.log(srt);
    // 8, 打印1000~2000年中所有的闰年, 每行4个
    let a = 0;
    for (i = 1000; i <= 2000; i++) {
      if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0) {
        document.write(i + " ");
        a++;
      }
      if (a % 4 == 0) {
        document.write("<br/>");
      }
    }
    
    //打印
    str = "";
    let a = 0;
    for (i = 1000; i <= 2000; i++) {
      if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0) {
        // document.write(i + ' ')
        str += i + " ";
        a++;
      }
      if (a % 4 == 0) {
        // document.write('<br/>')
        str += "\n";
      }
    }
    console.log(str);
    
    // 9, 求: 1-1/2+1/3-1/4 …  1/100的和
    let sum = 0;
    let sum1 = 0;
    let sum2 = 0;
    for (i = 1; i <= 100; i++) {
      if (i % 2 != 0) {
        sum1 += 1 / i;
      }
      if (i % 2 == 0) {
        sum2 -= 1 / i;
      }
      sum = sum1 + sum2;
    }
    console.log(sum);
    // 输入两个数,求两个数的最小公倍数(****)
    // 如: 9和6的最小公倍数是18,
    //  1, 先找出两个数中的最大数   // 9 -> 6*9
    //  2, 最大数++,找出能被两个数整除的数(退出循环)
    let a = prompt();
    let b = prompt();
    let c = 0;
    if (a > b) {
      c = a;
      console.log("最大数为" + a);
    } else {
      console.log("最大数为" + b);
      c = b;
    }
    while (c) {
      if (c % a == 0 && c % b == 0) {
        console.log("最小公倍数为" + c);
        break;
      }
      c++;
    }
    // 输入两个数n,a,如果n==3, a == 2;
    // 输出 2 + 22 + 222 的值。(不用输出式子)(****)
    // 如: n == 4, a == 3;
    // 输出 3 + 33 + 333 + 3333的值。
    // 提示: 1、n = 3,相加三次,每次相加比前一次相加的数,多一位
    //       2、每次多的这个位数的值为a,  3, 3*10+3(33), 33*10+3(333),...
    let n = prompt();
    let a = prompt();
    let sum = 0;
    let str = "";
    for (i = 0; i < n; i++) {
      str += a;
      sum += Number(str);
    }
    console.log(sum);
    
    // 11, 五位数中,对称的数称为回文数,找出所有的回文数。
    // 如: 12321
    for (let i = 10000; i < 100000; i++) {
      let a = parseInt(i / 10000);
      let b = parseInt((i / 1000) % 10);
      let c = parseInt((i / 10) % 10);
      let d = parseInt(i % 10);
      if (a == d && b == c) {
        console.log(i);
      }
    }
    
    // 13, 求1!+2!+3!+4!+5!
    let sum = 0;
    for (let i = 1; i <= 5; i++) {
      let sum1 = 1;
      for (let j = 1; j <= i; j++) {
        sum1 *= j;
      }
      sum += sum1;
    }
    console.log(sum);
    
    // 14, 找出所有的水仙花数,三位数,各位立方和等于该数本身。
    // 如: 153 = 1 ^ 3 + 5 ^ 3 + 3 ^ 3
    for (let i = 100; i < 1000; i++) {
      let a = parseInt(i / 100);
      let b = parseInt((i / 10) % 10);
      let c = parseInt(i % 10);
      if (i == Math.pow(a, 3) + Math.pow(b, 3) + Math.pow(c, 3)) {
        console.log(i);
      }
    }
    
    // 15, 输入任意两个数,如果第一个数小,从第一个数打印到第二个数,如果第二个数小,从第二个数打印到第一个数
    let a = prompt();
    let b = prompt();
    if (a < b) {
      console.log(a + " " + b);
    } else {
      console.log(b + " " + a);
    }
    
    // 16,  输入两个数,求两个数的最大公约数(*****)
    // 如: 12和8的最大公约数是4,
    // 提示: 能够同时整除两个数的最大数
    //  1, 先找出两个数中最小的那个数,
    //  2, 最小数--, 找出能被两个数整除的数(退出循环break)
    
    let a = prompt();
    let b = prompt();
    let min = 0;
    a < b ? (min = a) : (min = b);
    for (i = min; i > 0; i--) {
      if (a % i == 0 && b % i == 0) {
        console.log(i);
        break;
      }
    }
    

    相关文章

      网友评论

          本文标题:JS循环语句--篇7

          本文链接:https://www.haomeiwen.com/subject/kfywbdtx.html