当前位置:网站首页>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(","));
边栏推荐
- Selective sorting and bubble sorting [C language]
- gcc 编译选项
- MP3mini播放模块arduino<DFRobotDFPlayerMini.h>函数详解
- Esp8266 connects to bafayun (TCP maker cloud) through Arduino IED
- RT-Thread 线程的时间片轮询调度
- Mysqldump error1066 error solution
- Time slice polling scheduling of RT thread threads
- C语言函数之可变参数原理:va_start、va_arg及va_end
- Arm pc=pc+8 is the most understandable explanation
- Fashion-Gen: The Generative Fashion Dataset and Challenge 论文解读&数据集介绍
猜你喜欢
共用体(union)详解【C语言】
Comparison of solutions of Qualcomm & MTK & Kirin mobile platform USB3.0
ESP学习问题记录
程序员老鸟都会搞错的问题 C语言基础 指针和数组
Feature of sklearn_ extraction. text. CountVectorizer / TfidVectorizer
【ESP32学习-1】Arduino ESP32开发环境搭建
Reno7 60W super flash charging architecture
AMBA、AHB、APB、AXI的理解
MP3mini播放模块arduino<DFRobotDFPlayerMini.h>函数详解
MySQL占用内存过大解决方案
随机推荐
The dolphin scheduler remotely executes shell scripts through the expect command
Variable parameter principle of C language function: VA_ start、va_ Arg and VA_ end
GCC compilation options
C language callback function [C language]
Kaggle competition two Sigma connect: rental listing inquiries
高通&MTK&麒麟 手机平台USB3.0方案对比
RuntimeError: cuDNN error: CUDNN_STATUS_NOT_INITIALIZED
Who says that PT online schema change does not lock the table, or deadlock
Arm pc=pc+8 is the most understandable explanation
ESP8266通过Arduino IDE连接Onenet云平台(MQTT)
JS variable types and common type conversions
Kaggle competition two Sigma connect: rental listing inquiries (xgboost)
Dependency in dependencymanagement cannot be downloaded and red is reported
优先级反转与死锁
.elf .map .list .hex文件
Rough analysis of map file
1081 rational sum (20 points) points add up to total points
E-commerce data analysis -- User Behavior Analysis
选择法排序与冒泡法排序【C语言】
Feature of sklearn_ extraction. text. CountVectorizer / TfidVectorizer