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

边栏推荐
- Classification, understanding and application of common methods of JS array
- Apprentissage automatique - - régression linéaire (sklearn)
- Basic operations of databases and tables ----- creating data tables
- Machine learning -- linear regression (sklearn)
- Pytorch-温度预测
- . elf . map . list . Hex file
- Use of lists
- C语言函数之可变参数原理:va_start、va_arg及va_end
- [esp32 learning-2] esp32 address mapping
- RuntimeError: cuDNN error: CUDNN_ STATUS_ NOT_ INITIALIZED
猜你喜欢

Unit test - unittest framework

JS 函数提升和var变量的声明提升

Priority inversion and deadlock

ARM PC=PC+8 最便于理解的阐述

数据分析之缺失值填充(重点讲解多重插值法Miceforest)

Apprentissage automatique - - régression linéaire (sklearn)

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

Mysql database interview questions

JS變量類型以及常用類型轉換

程序员老鸟都会搞错的问题 C语言基础 指针和数组
随机推荐
uCOS-III 的特点、任务状态、启动
Pytoch implements simple linear regression demo
Gallery's image browsing and component learning
JS變量類型以及常用類型轉換
基于Redis的分布式ID生成器
Working principle of genius telephone watch Z3
Kconfig Kbuild
Page performance optimization of video scene
ESP学习问题记录
Embedded startup process
Amba, ahb, APB, Axi Understanding
Reno7 60W super flash charging architecture
vim命令行笔记
Programmers can make mistakes. Basic pointers and arrays of C language
Selective sorting and bubble sorting [C language]
Mp3mini playback module Arduino < dfrobotdfplayermini H> function explanation
Basic operations of databases and tables ----- creating data tables
[esp32 learning-1] construction of Arduino esp32 development environment
基於Redis的分布式ID生成器
Custom view puzzle getcolor r.color The color obtained by colorprimary is incorrect