当前位置:网站首页>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(","));
边栏推荐
- Symbolic representation of functions in deep learning papers
- Mp3mini playback module Arduino < dfrobotdfplayermini H> function explanation
- Characteristics, task status and startup of UCOS III
- C language, log print file name, function name, line number, date and time
- ESP8266通过Arduino IDE连接Onenet云平台(MQTT)
- OSPF message details - LSA overview
- Arduino get random number
- Pat 1097 duplication on a linked list (25 points)
- Missing value filling in data analysis (focus on multiple interpolation method, miseforest)
- Cannot change version of project facet Dynamic Web Module to 2.3.
猜你喜欢
【ESP32学习-2】esp32地址映射
Togglebutton realizes the effect of switching lights
优先级反转与死锁
arduino JSON数据信息解析
open-mmlab labelImg mmdetection
Kconfig Kbuild
Basic operations of databases and tables ----- classification of data
高通&MTK&麒麟 手机平台USB3.0方案对比
[Red Treasure Book Notes simplified version] Chapter 12 BOM
Amba, ahb, APB, Axi Understanding
随机推荐
Selective sorting and bubble sorting [C language]
Knowledge summary of request
There are three iPhone se 2022 models in the Eurasian Economic Commission database
Reno7 60W超级闪充充电架构
JS regular expression basic knowledge learning
【ESP32学习-1】Arduino ESP32开发环境搭建
Classification, understanding and application of common methods of JS array
PyTorch四种常用优化器测试
Detailed explanation of Union [C language]
The dolphin scheduler remotely executes shell scripts through the expect command
Pat 1097 duplication on a linked list (25 points)
[esp32 learning-1] construction of Arduino esp32 development environment
Custom view puzzle getcolor r.color The color obtained by colorprimary is incorrect
I2C bus timing explanation
VIM command line notes
电商数据分析--薪资预测(线性回归)
ESP学习问题记录
map文件粗略分析
Use of lists
FreeRTOS 任务函数里面的死循环