编辑代码

// //(1)字符串中出现次数最多的字符
//  const str  = 'asdladsssskjsdfk'
//  const obj={}
//  for ( var i =0;i<str.length;i++){
//      if(!obj[str.charAt(i)]){
//          obj[str.charAt(i)] = 1
//      }else{
//          obj[str.charAt(i)]++
//      }
//  }
//  console.log(obj)
//  var count = 0
//  var word= ''
//  for (var i in obj){
//      if(obj[i]>count){
//          count = obj[i]
//          word =i
//      }
//  }
//  console.log(word,count )


//数组中元素出现的次数 用reduce
// const arr  =['a','s','a','sdaf']
// const res = arr.reduce((pre,cur) =>{
//             if( cur in pre){
//                 pre[cur]++
//             } else{
//                 pre[cur] = 1
//             }
//             return pre
// },{})
// console.log(res)

//翻转字符串
// const str = '1sadf'
// const fn1 =item =>item.split('').reverse().join()
// const res = fn1(str)
// console.log(res)


// 数字转换为RMB
// const str =123234
// la = item =>{
//     var newitem  = item +''
//     const strArr =newitem.split('').reverse()
//     const res =[]
//     strArr.forEach((item,index) =>{
//         if(index%3===0 && index !==0 ){
//             res.push(',')
//         }
//         res.push(item)
//     })
//     return res.reverse().join('')
// }
// console.log(la(str))

// 排序算法
// var arr = [10,20,5,8,21,100,99]
// function test() {
//   for(var i = 0;i<arr.length-1;i++) {
//     for(var j = i+1; j < arr.length; j++) {
//      if(arr[i]>arr[j]){
//        var temp = arr[i]
//        arr[i] = arr[j]
//        arr[j] = temp
//      }
//     }
//   }
//   console.log(arr)   //[5, 8, 10, 20, 21, 99, 100]
// }test()

//从小到大冒泡排序 
// const arr = [1,2,3,4,5,3,3,5,6,7]
// const test = arr =>{
//    for (let i =0; i<arr.length-1;i++){
//        for (let j = i+1; j<arr.length;j++){
//            if(arr[i]>arr[j]){
//           const temp =arr[i]
//           arr[i] = arr[j]
//           arr[j] = temp
//            }

//        }
//    }
//    return arr

// }

// console.log(test(arr))

// 选择排序
// var arr = [10,18,1,6,9,20,7,90];
//     for( var j = 0; j <= arr.length - 1 - 1; j++){
//         // 第二个1 最开始的不用跟自己比较
//         var min = j;
//         for( var i = j + 1; i <= arr.length - 1; i++){
//             if(arr[min] > arr[i]){
//                 min = i;
//             }
//         }
//         if(min != j){
//             var m;
//             m = arr[j];
//             arr[j] = arr[min];
//             arr[min] = m;
//         }  
//     }
//     console.log(arr);

//从大到小
// const arr = [3, 2, 34, 57, 2, 24, 53, 3]
// for (let i = 0; i < arr.length - 2; i++) {
//     var max = i
//     for (let j = i + 1; j < arr.length; j++) {
//         if (arr[max] < arr[j]) {
//             max = j
//         }
//     }
//     if (max != i) {
//         var temp
//         temp = arr[i]
//         arr[i] = arr[max]
//         arr[max] = temp
//     }
// }
// const quchong = item => {
//     const newArr = []
//     for (let k = 0; k < item.length; k++) {
//         const res = newArr.indexOf(item[k])
//         if (res == -1) {
//             newArr.push(arr[k])
//         }
//     }
//     return newArr
// }
// console.log(quchong(arr))