Front-end development interview questions (fresh students) -- js articles

foreword

        Today's many Internet companies, especially some large companies, always like to ask various algorithm questions to test the logical thinking ability of programmers. These are the interview questions I did when I was applying for the job, and there are some algorithm questions collected online that have a relatively high appearance rate during the interview. Now I will share them with you, hoping to bring some help to some front-end development candidates. , Even if the questions asked during the interview are different from these, the ideas of these questions are universal.


Topic 1:

This is a 9*9 multiplication formula table, which is completely generated by algorithm. Please use javascript to complete the generation of such a multiplication formula table. The requirements are exactly the same as the picture, pay attention to the border, pay attention to the font, pay attention to all the details, and it is not allowed to use the table .


Description: Use document.write() to create HTML tags and add classes; recursively form a 9*9 multiplication table; then use css to add styles to the multiplication table to make it an isosceles triangle. (The problem of overlapping borders can be eliminated by setting the margin to a negative value)

JavaScript code:

document.write("<div class='box'>");		//最外层div
for (var i=1; i<=9; i++) {			//行
    document.write("<div>");		
    for (var j=1; j<=i; j++) {		        //列	
        document.write("<span class='column'>"+ i + "×" + j + "=" + i*j + "</span>");
    }
    document.write("</div>");
}
document.write("</div>");

CSS code:

.box {
     text-align: center;
 }
 .column {
     display: inline-block;
     width: 51px;
     height: 17px;
     font-size: 12px;
     border: 1px solid #E2E2E2;
     margin-right: -1px;
     margin-bottom: -1px;
     padding: 5px 10px;
}


Topic 2:

Through an input box, enter a custom array, such as 1, 4, 5, 23, 2, 17, 24, 10000000. Please sort him according to the middle high and the low side. The final result is 1, 4, 5, 23, 10000000, 24, 17, 2. The more accurate the algorithm, the better, please pay attention to the balance of the left and right data.

Analysis: It should be handled on a case-by-case basis. 1. If the length of the array is an even number, it is directly divided into two groups, the first group is sorted from small to large, the second group is sorted from large to small, and the two groups are spliced ​​and output. 2. If the length of the value is an odd number, take out the largest number, perform 1 operation on the remaining even-digit arrays, and then splicing the two obtained arrays with the largest number for output.

JavaScript code:

var cont = prompt("Please enter an array separated by commas");
var arr = cont.split(",");
var arr1 = [];
var arr2 = [];
var max =Math.max.apply(null, arr);
if (arr.length%2==0) {			        //偶数
    arrSplit(arr,arr1,arr2); //Call the sorting function	
    document.write(arr1.concat(arr2)); //splicing arr1 and arr2 arrays
}
else {// odd
    arr.sort(function(a, b){ //sort the array
        return b-a;
    });				
    var newArr = arr.slice(1); //Remove the largest number to get an array of even digits					
    arrSplit(newArr,arr1,arr2)				
    arr2.unshift(max); //Insert the largest number into arr2	
    document.write(arr1.concat(arr2));
    }
			
function arrSplit(arr,arr1,arr2){ //Encapsulate a function that splits and sorts an even-digit array			
    var Array = []; //Define an array to store the return values ​​of arr1 and arr2
    for (var i=0; i<arr.length/2; i++) { //split into two arrays
        arr1[i]= arr[i];				
        arr2[i]= arr[i+arr.length/2];			
    }			
    arr1.sort(function (a, b){ //Ascending
        return a-b;				
    });
    arr2.sort(function (c, d){ //descending    
        return d-c;				
    });
    Array[0]=arr1;
    Array[1]=arr2;
    return Array; //return arr1 and arr2 array
}


topic 3

Remove duplicates in an array and sort in ascending order.

Method 1: Conventional method

function test(arr) {
    var result = [];
    for (var i=0,len=arr.length; i<len; i++) {
        if ( result.indexOf(arr[i]) == -1 ){
            result.push(arr[i]);
        }
    }
    return result.sort(function (a,b) {
        return a-b;
    });
}
console.log(test([10,1,3,5,5,8])); //output: [ 1, 3, 5, 8, 10 ]

Method 2: ES6 method, use set to deduplicate

function f(arr) {
    let newArr = [...new Set(arr)];
    return newArr.sort(function (a,b) {
        return a-b;
    })
}
console.log(f([10,1,3,5,5,8]));


Topic 4

Native js implements the Fibonacci sequence.

Explanation: The Fibonacci sequence is introduced by the example of the breeding of rabbits, so it is also called "rabbit sequence", which refers to such a sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34, ...; Mathematically, the Fibonacci sequence is defined recursively as follows: F(1)=1, F(2)=1, F(n)= F(n-1)+ F( n-2) ( n>2, n∈N* ).

Method 1: Recursive method

function f(n) {
    if ( n==1 || n==2 ){
        return 1;
    }else{
        return f(n-1) + f(n-2);
    }
}
console.log(f(6));

Method 2: Dynamic programming method (performance is optimized)

function fibonacci(n) {
    let n1 = 1,
        n2 = 1,
        sum = 1;
    for(let i = 3; i <= n; i += 1) {
        sum = n1 + n2;
        n1 = n2; // move one digit backward
        n2 = sum
    }
    return sum
}
console.log(fibonacci(5));


See more js algorithm questions: https://blog.csdn.net/mr_javascript/article/details/79779598

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325570971&siteId=291194637