博客列表 >字符串、数组函数、分支和循环

字符串、数组函数、分支和循环

橙絮圆
橙絮圆原创
2021年07月11日 17:54:41485浏览

字符串、数组函数、分支和循环

作业标题:0709作业
作业内容:

  1. 实例演示常用的字符串和数组函数(自选5个即可)。
    2.实例演示分支与循环,将课堂上列支的所有场景全部演示一遍。
  • 字符串和数组函数

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8" />
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    7. <title>字符串方法</title>
    8. </head>
    9. <body>
    10. <script>
    11. //concat():字符串拼装
    12. str = "html" + ", css" + ", js";
    13. console.log(str);
    14. str = "html".concat(", css", ", js");
    15. console.log(str);
    16. //slice():从子串
    17. str = "Hello PHP.CN";
    18. //slice():从子串
    19. str = "Hello PHP.CN";
    20. str.slice(0, 5);
    21. //Hello
    22. //Hello
    23. //substr()
    24. res = str.substr(0, 5);
    25. console.log(res);
    26. res = str.substr(-6, 3);
    27. console.log(res);
    28. let arr = [];
    29. //push:在尾部添加
    30. console.log(arr.push("a", "c", "d"));
    31. console.log(arr);
    32. //pop()从尾部删除
    33. console.log(arr.pop());
    34. console.log(arr);
    35. </script>
    36. </body>
    37. </html>

  • 分支与循环

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8" />
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    7. <title>分支、循环</title>
    8. <!-- if elseif else,switch,三元运行? :,while, do while, for, for in对象 for of数组 -->
    9. </head>
    10. <body>
    11. <script>
    12. //单分支
    13. let score = 39;
    14. if (score >= 60) {
    15. console.log("及格");
    16. }
    17. //双分支
    18. score = 59;
    19. if (score >= 60) {
    20. console.log("及格");
    21. } else {
    22. console.log("不及格");
    23. }
    24. //多分支
    25. score = 100;
    26. if (score >= 60 && score <= 70) {
    27. console.log("及格");
    28. } else if (score >= 70 && score <= 90) {
    29. console.log("不错");
    30. } else if (score > 100 || score < 0) {
    31. console.log("非法字符");
    32. } else if (score >= 90 && score <= 100) {
    33. console.log("优秀");
    34. } else {
    35. console.log("不及格");
    36. }
    37. //简化多分支
    38. score = 50;
    39. switch (true) {
    40. case score >= 60 && score <= 70:
    41. console.log("及格");
    42. break;
    43. case score >= 70 && score <= 90:
    44. console.log("不错");
    45. break;
    46. case score > 100 || score < 0:
    47. console.log("非法字符");
    48. break;
    49. case score >= 90 && score <= 100:
    50. console.log("优秀");
    51. break;
    52. default:
    53. console.log("不及格");
    54. }
    55. //while循环
    56. arr = [1, 2, 3, 4, 5];
    57. let i = 0;
    58. while (i < arr.length) {
    59. console.log(arr[i]);
    60. i++;
    61. }
    62. //do while循环
    63. arr1 = [1, 2, 3, 4, 5];
    64. i = 0;
    65. do {
    66. console.log(arr1[i]);
    67. i++;
    68. } while (i < arr1.length);
    69. //for循环
    70. arr2 = [1, 2, 3, 4, 5];
    71. for (let a = 0; a < arr2.length; a++) {
    72. console.log(arr1[a]);
    73. }
    74. //循环对象
    75. const user = {
    76. id: 1,
    77. name: "admin",
    78. email: "admin@php.cn",
    79. };
    80. for (let key in user) {
    81. console.log(user[key]);
    82. }
    83. //循环数组
    84. let arr3 = [1, 2, 3, 4, 5, 6, 7, 8];
    85. for (let value of arr3) {
    86. console.log(value);
    87. }
    88. </script>
    89. </body>
    90. </html>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议