Js 基础算法

Miss.Ceel小于 1 分钟JavaScript算法

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

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. ** 不借助第三变量, 交换值.

function swop (a, b) {
    a = a - b;
    b = a + b;
    a = b - a;
    return [a, b]
}

console.log(45, 68)

上次编辑于:
贡献者: misszhangxm
Loading...