登录
首页 >  文章 >  前端

查找数组中的最大数字 - JavaScript

来源:dev.to

时间:2024-10-15 16:57:40 168浏览 收藏

偷偷努力,悄无声息地变强,然后惊艳所有人!哈哈,小伙伴们又来学习啦~今天我将给大家介绍《查找数组中的最大数字 - JavaScript》,这篇文章主要会讲到等等知识点,不知道大家对其都有多少了解,下面我们就一起来看一吧!当然,非常希望大家能多多评论,给出合理的建议,我们一起学习,一起进步!

查找数组中的最大数字 - JavaScript

使用 javascript 从正整数数组中查找最大数字

解决方案

//findmaxnumber.js
function findmaxnumber(numbers) {
//initializes result to track the maximum number, starting at 0.
  let result = 0;

  //stores the length of the numbers array for use in the loop.
  const numberslength = numbers.length;

  //check if the array is not empty before processing it.
  if (numberslength > 0) {

    //loops through each number in the array.
    for (let i = 0; i < numberslength; i++) {

      //compare each number to the result to check if it's larger.
      if (numbers[i] > result) {

        //updates result with the current number if it's the largest
        result = numbers[i];
      }
    }
    console.log(result);
    return result;
  }

  return result;
}

findmaxnumber([5, 33, 47, 103]);

结果

> 103

文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《查找数组中的最大数字 - JavaScript》文章吧,也可关注golang学习网公众号了解相关技术文章。

声明:本文转载于:dev.to 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>