当前位置:网站首页>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(","));

边栏推荐
- ES6 grammar summary -- Part I (basic)
- open-mmlab labelImg mmdetection
- STM32 how to locate the code segment that causes hard fault
- 优先级反转与死锁
- [Red Treasure Book Notes simplified version] Chapter 12 BOM
- ESP学习问题记录
- Arduino gets the length of the array
- Esp8266 connects to bafayun (TCP maker cloud) through Arduino IED
- Common properties of location
- Analysis of charging architecture of glory magic 3pro
猜你喜欢

AMBA、AHB、APB、AXI的理解

open-mmlab labelImg mmdetection

Fashion-Gen: The Generative Fashion Dataset and Challenge 论文解读&数据集介绍

E-commerce data analysis -- User Behavior Analysis

The dolphin scheduler remotely executes shell scripts through the expect command

Gallery's image browsing and component learning

Analysis of charging architecture of glory magic 3pro

PyTorch四种常用优化器测试

Générateur d'identification distribué basé sur redis

Machine learning -- linear regression (sklearn)
随机推荐
VSCode基础配置
ES6 grammar summary -- Part I (basic)
Feature of sklearn_ extraction. text. CountVectorizer / TfidVectorizer
level16
MySQL占用内存过大解决方案
JS變量類型以及常用類型轉換
JS regular expression basic knowledge learning
MP3mini播放模块arduino<DFRobotDFPlayerMini.h>函数详解
JS数组常用方法的分类、理解和运用
Navigator object (determine browser type)
Comparison of solutions of Qualcomm & MTK & Kirin mobile platform USB3.0
数据分析之缺失值填充(重点讲解多重插值法Miceforest)
STM32 how to locate the code segment that causes hard fault
Important methods of array and string
Understanding of AMBA, AHB, APB and Axi
IOT system framework learning
Programmers can make mistakes. Basic pointers and arrays of C language
Working principle of genius telephone watch Z3
Kconfig Kbuild
Pytoch temperature prediction