当前位置:网站首页>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(","));
边栏推荐
- The first simple case of GNN: Cora classification
- 基於Redis的分布式ID生成器
- Arduino uno R3 register writing method (1) -- pin level state change
- Kaggle competition two Sigma connect: rental listing inquiries
- js 变量作用域和函数的学习笔记
- Esp8266 connects to bafayun (TCP maker cloud) through Arduino IED
- PyTorch四种常用优化器测试
- GCC compilation options
- uCOS-III 的特点、任务状态、启动
- open-mmlab labelImg mmdetection
猜你喜欢
随机推荐
Basic operations of databases and tables ----- view data tables
Imgcat usage experience
数据分析之缺失值填充(重点讲解多重插值法Miceforest)
共用体(union)详解【C语言】
小天才电话手表 Z3工作原理
ESP8266使用arduino连接阿里云物联网
History object
VIM command line notes
arduino UNO R3的寄存器写法(1)-----引脚电平状态变化
Understanding of AMBA, AHB, APB and Axi
Priority inversion and deadlock
几个关于指针的声明【C语言】
ESP8266通过Arduino IDE连接Onenet云平台(MQTT)
機器學習--線性回歸(sklearn)
Fashion Gen: the general fashion dataset and challenge paper interpretation & dataset introduction
MP3mini播放模块arduino<DFRobotDFPlayerMini.h>函数详解
Pytoch implements simple linear regression demo
Comparison of solutions of Qualcomm & MTK & Kirin mobile platform USB3.0
Learning notes of JS variable scope and function
Missing value filling in data analysis (focus on multiple interpolation method, miseforest)