Today, I'll share with you some basic algorithms commonly used in js. No more nonsense, just go to the code.

Keywords: Javascript

Today, I want to share with you some basic algorithms commonly used in js.

1. The order of two digits exchange

  var a = 2,b=4
  function fun(a,b){
    b = b - a ;// a = 2 ; b = 2
    a = a + b // a = 4 ; b = 2;
    b = a - b;// a = 4 ; b = 2
    return [a,b]
  }
fun(a,b)   // a = 4 ;b =  2

2. Object sorting, the location of the id sorting object in the installation object;

  var arr = [
      { nama: 'a', id: 55 },
      { nama: 'b', id: 39 },  
      { nama: 'c', id: 59 },
  ]
  var newarr = arr.sort((a,b)=>{
     return a.id - b.id;
  })

  console.log(newarr)
//  {nama: "b", id: 39}
//  {nama: "a", id: 55}
//  {nama: "c", id: 59}

3. Bubble Sorting

function fun(arr) {
  for (let i = 0; i < arr.length - 1; i++) {
    for (let j = 0; j < arr.length - i - 1; j++) {
      if (arr[j] > arr[j + 1]) {
        let temp = arr[j + 1];
        arr[j + 1] = arr[j];
        arr[j] = temp;
      }
    }
  }
  return arr;
}
console.log(fun([1, 5, 2, 8, 3, 7])); // [1, 2, 3, 5, 7, 8]

4. Random occurrence of different numbers

function fun (num){
  var arr = [];
  while(arr.length < num){
    var s = parseInt(Math.random() * 100);
    // If there is no such item in the array, it can be entered.
    if(!arr.includes(s)){
      arr.push(s)
    }
  }
  return arr;
}
//  If you input several items, you will output several different arrays.
fun(3)

5. Interchange between case and case of strings

function fun (n){
  let str = ''
  for(let i = 0;i < n.length;i++){
    if(n[i] == n[i].toUpperCase()){
      str += n[i].toLowerCase()
    }else{
      str += n[i].toUpperCase();
    }
  }
  return str 
}
fun('AbCd') //  aBcD

6. Array Random Disturbance

function fun(arr){
  return arr.sort( ()=>{
    return Math.random() > 0.5 ? 1 : -1
  })
}
fun([1,2,3,4,5])

7. Flattening Arrays - Converting Multidimensional Arrays to One-Dimensional Arrays

function fun (arr){
  var result = [];
  arr.forEach(item =>{
    if(Array.isArray(item)){
      result.push(...fun(item  ))
    }else{
      result.push(item)
    }
  })
  return result
}
fun([1,[2,[3,[4,[5]],6,[7]]]]); //  [1, 2, 3, 4, 5, 6, 7]

8. Array de-weighting

function fun(arr){
  var newarr = [];
  for(let i = 0; i < arr.length;i++){
    if(!newarr.includes(arr[i])){
      newarr.push(arr[i])
    }
  }
  return newarr;
}
fun([1,1,1,2,3,3]) // [1, 2, 3]

9. Delete the tail of the array and add it to the front.

//  User does not enter, default 1 item;
function fun(arr,num = 1){
  for(let i = 0 ; i < num;i++){
    arr.unshift(arr.pop())
  }
  return arr;
}
fun([1,2,3,4]  //  [4, 1, 2, 3]

10. Statistics of the number of occurrences of characters;

function fun (arr){
  var obj = {};
  for(let i= 0 ; i <arr.length;i++){
    if(obj.hasOwnProperty(arr[i])){
      obj[arr[i]]++;
    }else{
      obj[arr[i]] = 1;
    }
  }
  let cont = 0, num;
  for(let k in obj){
    if(obj[k] > cont){
       cont = obj[k];
       num = k 
    }
  }
  return cont  //  The most frequent occurrences
  return num // Numbers that appear most frequently
  return obj // Statistics of the number of occurrences of all characters
}

fun('1223334444')

11. Delete arrays that appear more than 2 times.

function fun (arr){
  var obj = {};
  for(let i= 0 ; i <arr.length;i++){
    if(obj.hasOwnProperty(arr[i])){
      obj[arr[i]]++;
    }else{
      obj[arr[i]] = 1;
    }
  }
  let newarr = [];
  for(let k in obj){
    if(obj[k] <= 2){
      newarr.push(obj[k])
    }
  }
  return newarr;
}
fun([1,2,2,3,3,3,4,4,4,4]) //   [1, 2]

 

If you like, welcome to pay attention to "front-end pseudo uncle", I will share your front end learning knowledge for you.

Posted by pppppp on Tue, 01 Oct 2019 00:20:39 -0700