首页 > 其他分享 >js find the maximum and minimum values in an array All In One

js find the maximum and minimum values in an array All In One

时间:2022-09-28 12:15:30浏览次数:68  
标签:arr const min max 37 maximum js minimum Math

js find the maximum and minimum values in an array All In One

js 找出数组中的最大值与最小值 All In One

number / number string

build in methods Math.max & Math.min

const arr =  [9, 1, 3, 7, 12, 37, 23];

const max = Math.max(...arr);
const min = Math.min(...arr);

console.log(`max =`, max);
console.log(`min =`, min);
// max = 37
// min = 1

const arr =  ['9', '1', '3', '7', '12', '37', '23'];

const max = Math.max(...arr);
const min = Math.min(...arr);

// OR
const max = Math.max(...arr.map(Number));
const min = Math.min(...arr.map(Number));

console.log(`max =`, max);
console.log(`min =`, min);
// max = 37
// min = 1

sort

const arr =  [9, 1, 3, 7, 12, 37, 23];

// arr.sort();
// [1, 12, 23, 3, 37, 7, 9] ❌

//  arr.sort((a, b) => a -  b > 0 ? 1 :  -1);
//  [1, 3, 7, 9, 12, 23, 37] ✅

arr.sort((a, b) => a -  b);
//  [1, 3, 7, 9, 12, 23, 37] ✅

const max = arr[arr.length - 1];
const min = arr[0];

console.log(`max =`, max);
console.log(`min =`, min);
// max = 37
// min = 1


string

not work for string ❌

const arr =  ['c', 'b', 'a', 'c1', 'b2', 'a3', 'abc'];

const max = Math.max(...arr);
const min = Math.min(...arr);

console.log(`max =`, max);
console.log(`min =`, min);

// max = NaN
// min = NaN

custom sort function ✅

const arr =  ['c', 'b', 'a', 'c1', 'b2', 'a3', 'abc'];

// dict

'c'.charCodeAt(0);
// 99

'cba'.charCodeAt(2);
// 97


'cba'.charAt();
// 'c'
'cba'.charAt(2);
// 'a'

String.fromCharCode(97, 98, 99);
// 'abc'

refs

numbers ✅

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/min

strings / chars

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode



©xgqfrms 2012-2020

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载

标签:arr,const,min,max,37,maximum,js,minimum,Math
From: https://www.cnblogs.com/xgqfrms/p/16737535.html

相关文章

  • JS 算法
    1、js统计一个字符串出现频率最高的字母/数字letstr='absafdaf234234323232'leta=[...str]//alert(ainstanceofArray)conststrChar=......
  • 64. Minimum Path Sum
    Givenamxngridfilledwithnon-negativenumbers,findapathfromtoplefttobottomrightwhichminimizesthesumofallnumbersalongitspath.Note:Youc......
  • JS数组去重
    1、方法<!--*@Descripttion:数组去重*@version:0.0.1*@Author:PengShuai*@Date:2022-09-2613:16:04*@LastEditors:PengShuai*@LastEditTime:202......
  • js 倒计时按钮可用
    HTML<aclass="btnbtn-default"id="enterBtn"disabledstyle="margin-top:2rem"href="legalnewform.html">已知晓,进入法律咨询<spanid="countDown"styl......
  • 如何学习ReactJS:初学者完整指南
    英文| https://www.geeksforgeeks.org/how-to-learn-reactjs-a-complete-guide-for-beginners/?ref=rp翻译 |web前端开发(ID:web_qdkf)每个前端开发人员和Web开发人员都知......
  • 11._jsp_el_jstl
    ......
  • vue2.x引入threejs
    @目录vue2.x引入threejsnpm安装使用指定版本:其他插件实例强调vue2.x引入threejsnpm安装npminstallthree使用指定版本:npminstallthree@<版本号>其他插件因为本......
  • js各种方法
    判断类型、判断数据是否有值typeofletobj={}typeofobj===Object//根据typeof判断对象也不太准确表达式返回值typeofundefined......
  • JSON 语法
    JSON语法规则JSON语法是JavaScript对象表示语法的子集。数据在名称/值对中数据由逗号 , 分隔使用斜杆 \ 来转义字符大括号 {} 保存对象中括号 [] 保存......
  • js中的apply方法并模拟实现自己的apply方法
    apply方法定义call()方法,在mdn中的定义:apply()方法调用一个具有给定this值的函数,以及以一个数组(或类数组对象)的形式提供的参数。(它的作用和使用和call方法一致,唯一的......