JavaScript基础-数组-求和、最值、筛选、去0、根据数据生成矩形图

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // 需求:求数组[2, 6, 1, 7, 4] 里面所有元素的和以及平均值
        let arr = [2, 6, 1, 7, 4]
        let sum = 0
        let average = 0

        for(let i = 0; i < arr.length; i++){
            sum += arr[i]
        }
        average = sum / arr.length

        console.log('所有元素的和', sum)
        console.log('平均值', average)

    </script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // 需求:求数组[2, 6, 1, 77, 52, 25, 7] 中的最大值
        // 分析:
        // ①:声明一个保存最大元素的变量 max。
        // ②:默认最大值可以取数组中的第一个元素。
        // ③:遍历这个数组,把里面每个数组元素和 max 相比较。
        // ④:如果这个数组元素大于max 就把这个数组元素存到 max 里面,否则继续下一轮比较。
        // ⑤:最后输出这个 max
        let arr = [2, 6, 1, 77, 52, 25, 7]
        let max = arr[0]

        for(let i = 1; i < arr.length; i++){
            if(max < arr[i]){
                max = arr[i]
            }
        }
        console.log('数组[2, 6, 1, 77, 52, 25, 7] 中的最大值', max)

    </script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // 需求:将数组[2, 0, 6, 1, 77, 0, 52, 0, 25, 7] 中大于等于 10 的元素选出来,放入新数组
        // 分析:
        //     ①:声明一个新的数组用于存放新数据newArr
        //     ②:遍历原来的旧数组, 找出大于等于 10 的元素
        //     ③:依次追加给新数组 newArr
        let arr = [2, 0, 6, 1, 77, 0, 52, 0, 25, 7]
        let newArr = []
        for(let i = 0; i < arr.length; i++){
            if(arr[i] >= 10){
                newArr.push(arr[i])
            }
        }
        console.log('大于等于 10 的元素')
        for(let i = 0 ; i < newArr.length; i++){
            console.log(newArr[i])
        }
    </script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // 需求:将数组[2, 0, 6, 1, 77, 0, 52, 0, 25, 7] 中的 0 去掉后,形成一个不包含 0 的新数组
        // 分析:
        //     ①:声明一个新的数组用于存放新数据newArr
        //     ②:遍历原来的旧数组, 找出不等于0的元素
        //     ③:依次追加给新数组 newArr
        let arr = [2, 0, 6, 1, 77, 0, 52, 0, 25, 7]
        let newArr = []

        for(let i = 0; i < arr.length; i++){
            if(arr[i] != 0){
                newArr.push(arr[i])
            }
        }
        
        for(let i = 0; i < newArr.length; i++){
            console.log(newArr[i])
        }
    </script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>根据数据生成柱状图</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }

      .box {
        display: flex;
        width: 700px;
        height: 300px;
        border-left: 1px solid pink;
        border-bottom: 1px solid pink;
        margin: 50px auto;
        justify-content: space-evenly;
        align-items: flex-end;
        text-align: center;
      }

      .box > div {
        display: flex;
        width: 50px;
        background-color: pink;
        flex-direction: column;
        justify-content: space-between;
      }

      .box div span {
        margin-top: -20px;
      }

      .box div h4 {
        margin-bottom: -35px;
        width: 70px;
        margin-left: -10px;
      }
    </style>
  </head>

  <body>
    <!-- 
    <div class="box">

      <div style="height: 33px;">
        <span>33</span>
        <h4>第1季度</h4>
      </div>

      <div style="height: 22px;">
        <span>22</span>
        <h4>第2季度</h4>
      </div>
      <div style="height: 33px;">
        <span>33</span>
        <h4>第3季度</h4>
      </div>
      <div style="height: 33px;">
        <span>44</span>
        <h4>第4季度</h4>
      </div>

    </div>
    -->

    <script>
      /* ========第一部分:收集用户输入数据================== */
      // 3. 准备一个数组,用于存储用户输入的数据
      let array = []
      // 1. 通过循环控制弹窗次数
      for (let index = 0; index < 2; index++) {
        // 2. 获取弹窗用户输入的数据,可以转换成数值类型
        let res = Number(prompt(`请输入第 ${index + 1} 季度数据`))
        // 4. 把用户输入的数据存到数组中
        array.push(res)
      }
      /* ===========第二部分,根据数组生成柱状图============= */
      debugger
      // 先准备个 .box 盒子,作为柱状图分区
      document.write(`<div class="box">`)
      // 遍历改数组,根据数据生成4个柱形图,渲染打印到页面中
      for (let index = 0; index < array.length; index++) {
        // 重复输出柱子 div
        document.write(`
          <div style="height: ${array[index]}px;">
            <span>${array[index]}</span>
            <h4>第${index + 1}季度</h4>
          </div>
        `)        
      }
      // 给 <div class="box"> 的结束标签
      document.write(`</div>`)
    </script>
  </body>
</html>

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Q _Q

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值