전단 면접 손찢기 시리즈

3284 단어

1,천 비트

//      
number.toLocaleString();

//  
function commafy(num) {
    return num && num 
    .toString()
    .replace(/(\d)(?=(\d{3})+\.)/g, function($1, $2){
        return $2 + ',';
    })  
}

//      
function commafy(num) {
    var arr = [];
    var array = [];
    var str = num.split('.')[0].toString();
    array.push(num.split('.')[1]);
    var iNum = str.length % 3;
    while(iNum !==0){
      arr.push(str.substring(0, iNum));
      iNum = 0;
    }
    var j = str.length % 3;
    for(var i =0 ;i

2, 무작위 정렬

//   ,    
var arr = [1,2,3,4,5,6,7,8,9,10];
function random(arr) {
    for(var i = 0;i0) {
        var randomIndex = parseInt(Matn.random()*arr.length);
        newArray.push(arr[randomIndex]);
        arr.splice(randomIndex, 1);
    }
    return newArray;
}

//   ,  arr.sort
arr.sort(function () {
    return Math.random() - 0.5;
    })

3, js 구현 상속

//    
function Parent(){
    this.name = "IURUO";
}
function Child() {
    this.age = 21;
}

Child.prototype = new Parent();//      

4, 객체 작성 방법

1,     
var obj = {
    name:'IU',
    age:'21'
}

2,function         
function Human() {}
var Ming = new Human();
Ming.name = 'OO';
Ming.age = 18;
Ming.doing = function () {
    console.log(Ming.age);
}

3,function        
function Pet(name,age,hobby) {
    this.name = name;
    this.age = age;
    this.miss = function () {
        console.log('  '+this.name+',   '+this.hobby+',     ');
    }
}

//4,    
var Wang = new Object();
Wang.age = 22333;
Wang.work = function () {
    console.log(this.age);
}

//5,      
function Dog () {}
Dog.prototype.name = 'MV';
Dog.prototype.work = () => {
    cosole.log(this.name +'     ');
}

//6,      
function Car (name,price) {
    this.name =name;
    this.price = price;
}
Car.portotype.cell = () =>{
    console.log('     '+this.price);
}

5, 반복 요소 반환
function findRepeat(arr) {
    var temp = [];
    arr.forEach(function(item){
        (arr.indexOf(item) !== arr.lastIndexOf(item) && arr.indexOf(item) !== -1) && temp.push(item);
    })
    return Array.from(new Set(temp));
}

6, 정렬 직접 삽입
function insertSort(arr){
    for(let i=1; i < arr.length; i++){
    let temp = arr[i];
    let j;
    for(j=i-1; j >= 0; j--){
        if(arr[j] > temp) {
            arr[j+1] = arr[j];
        } else {
            break;
        }        
      }
    arr[j+1] = temp;
   }
    return arr;
}

7, 빠른 정렬
function quickSort(arr) {
    if(arr.length <= 1){
        return arr;
    }
    let pivot = Math.floor((arr.length-1)/2);
    let val = arr[pivot];
    let less = [];
    let more = [];
    arr.splice(pivot,1);
    arr.forEach(function(item){
      item > val ? more.push(item) : less.push(item);  
    })
    return arguments.callee(less).concat([val],arguments.callee(more));
}

좋은 웹페이지 즐겨찾기