当前位置:网站首页>JS Title: input array, exchange the largest with the first element, exchange the smallest with the last element, and output array.
JS Title: input array, exchange the largest with the first element, exchange the smallest with the last element, and output array.
2022-07-06 12:13:00 【Aboci Bang】
var arr34 = [1, 4, 32, 4, 5, 7, 8];// Actual output [32,4,8,4,5,7,1]
var arr_new_34 = [].concat(arr34);// Deep copy
arr_new_34.sort(function (a, b) {
return a > b ? 1 : -1; });// Sort to find the maximum and minimum value
var max = arr_new_34[arr_new_34.length - 1];
var min = arr_new_34[0];
var maxIndex = arr34.indexOf(max);// Get the maximum value in the subscript of the original array
[arr34[0], arr34[maxIndex]] = [arr34[maxIndex], arr34[0]];// Swap the maximum value with the first element -- Deconstruct assignment
//*** Be careful If the first time you exchange positions is to exchange the maximum and minimum positions here The subscript of the minimum value has changed Turned into 2 Turned out to be 0
// So the subscript of the minimum value has to wait until the position is obtained after the exchange
var minIndex = arr34.indexOf(min);// Get the minimum value in the subscript of the original array
[arr34[arr34.length - 1], arr34[minIndex]] = [arr34[minIndex], arr34[arr34.length - 1]];// Swap the minimum value with the first element
console.log(arr34.join(","));

边栏推荐
猜你喜欢

arduino JSON数据信息解析

Mp3mini playback module Arduino < dfrobotdfplayermini H> function explanation

Feature of sklearn_ extraction. text. CountVectorizer / TfidVectorizer

E-commerce data analysis -- User Behavior Analysis
![Several declarations about pointers [C language]](/img/9b/ace0abbd1956123a945a98680b1e86.png)
Several declarations about pointers [C language]

Kaggle competition two Sigma connect: rental listing inquiries (xgboost)

MySQL占用内存过大解决方案

Vscode basic configuration

【ESP32学习-2】esp32地址映射

MySQL takes up too much memory solution
随机推荐
RT-Thread的main线程“卡死”的一种可能原因及解决方案
共用体(union)详解【C语言】
ESP8266通过Arduino IDE连接Onenet云平台(MQTT)
【ESP32学习-2】esp32地址映射
ESP8266通过arduino IED连接巴法云(TCP创客云)
Esp8266 uses Arduino to connect Alibaba cloud Internet of things
Mysql database interview questions
E-commerce data analysis -- salary prediction (linear regression)
OSPF message details - LSA overview
Inline detailed explanation [C language]
Redis based distributed locks and ultra detailed improvement ideas
Arm pc=pc+8 is the most understandable explanation
C语言,log打印文件名、函数名、行号、日期时间
ESP learning problem record
ES6语法总结--下篇(进阶篇 ES6~ES11)
arduino UNO R3的寄存器写法(1)-----引脚电平状态变化
1081 rational sum (20 points) points add up to total points
Pytorch four commonly used optimizer tests
JS變量類型以及常用類型轉換
js 变量作用域和函数的学习笔记