Js 基础算法

1. 返回字符串中字符出现次数最多的那字符

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
var str = 'tecasdasdsfgjoertioerldgibidflksdsssssllllldnwekroiwrdslfashnology';

String.prototype.maxnum = function () {
if(this.length <= 1) {
return this
};
let arr = this.split('');
let josn = {};
for(let i = 0, L = this.length; i < L; i++){
if(!josn[arr[i]]){
josn[arr[i]] = 1
} else {
josn[arr[i]] += 1
}
}
let num = 0;
let wrop = '';
for (let item in josn) {
if(josn[item] > num) {
num = josn[item];
wrop = item;
}
}
return wrop;
}

console.log(str.maxnum())

2. 不借助第三变量, 交换值.

1
2
3
4
5
6
7
8
function swop (a, b) {
a = a - b;
b = a + b;
a = b - a;
return [a, b]
}

console.log(45, 68)
-------------本文结束感谢您的阅读-------------