当前位置:网站首页>js案例之求最大值,反转数组,冒泡排序
js案例之求最大值,反转数组,冒泡排序
2022-04-23 06:25:00 【小白蛋挞】
//求最大值
function getMax() {
var max = arguments[0];
for (var i = 1; i <
arguments.length; i++) {
if (arguments[i] > max) {
max = arguments[i];
}
}
return max;
}
console.log(getMax(1, 2, 3));
console.log(getMax(1, 2, 3, 4, 5));
//反转数组
function reserve() {
var newarr = [];
for (var i = arguments.length - 1; i >= 0; i--) {
newarr[newarr.length] = arguments[i];
}
return newarr;
}
var arr = reserve(1, 3, 4);
console.log(arr);
// 冒泡排序
function bubble(arr) {
for (var i = 0; i < arr.length; i++) {
for (var j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
var temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
return arr;
}
var arr1 = bubble([3, 2, 5, 6, 1]);
console.log(arr1);
版权声明
本文为[小白蛋挞]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_61547956/article/details/124127738
边栏推荐
猜你喜欢
随机推荐
[Educational Codeforces Round 80] 解题报告
12.约束
SAP pi / PO rfc2soap publishes RFC interface as WS example
js中对象的三种创建方式
SAP TRANSLATE使用数据对象掩码示例
【TED系列】一个习惯是如何改变我的一生
NPM installation stepping pit
组合数求解与(扩展)卢卡斯定理
12. Constraints
Nacos / sentinel gateway current limiting and grouping (code)
SAP PI/PO rfc2RESTful 发布rfc接口为RESTful示例(Proxy间接法)
npm 安装踩坑
页面实时显示当前时间
安装配置淘宝镜像npm(cnpm)
10. Update operation
BTREE, B + tree and hash index
Implementation of MySQL persistence
8. Paging query
MySQL storage engine
redis连接出错 ERR AUTH <password> called without any password configured for the default user.