[high frequency written interview in front field] question 10: JavaScript related

Keywords: Javascript Front-end Interview regex

catalogue

1. Known array var stringArray = ["This", "is", "Baidu", "Campus"]

2. The string foo = "get element by ID" is known. Write function to convert it into hump representation

3. Known array var numberArray = [3,6,2,4,1,5]; Apply array API for operation

4. Output today's date in YYYY-MM-DD format

5. Use regular expressions to replace {$id} with 10 and {$name} with Tony in the string "{$id}{$name}"

6. In order to ensure the safety of page output, we often need to escape some special characters. Please write a function escapeHtml to escape <, >, & "and"

7. Foo = foo|bar, what does this line of code mean? Why is it written like this?

8. The output result of the following code is?

9. Use js to randomly select 10 numbers between 10 – 100, store them in an array, and sort them

10. Merge the two arrays and delete the second element

1. Known array var stringArray = ["This", "is", "Baidu", "Campus"]

Alert "This is Baidu Campus":

alert(stringArray.join(" "))  //Converts an array to a string separated by spaces

2. The string foo = "get element by ID" is known. Write function to convert it into hump representation

function combo(msg){
    var arr=msg.split("-");
    for(var i=1;i<arr.length;i++){
        arr[i]=arr[i].charAt(0).toUpperCase()+arr[i].substr(1,arr[i].length-1);
    }
    msg=arr.join("");
    return msg;
}

3. Known array var numberArray = [3,6,2,4,1,5]; Apply array API for operation

(1) Invert the array and output [5,1,4,2,6,3];

numberArray.reverse( )

(2) Realize the descending arrangement of the array and output [6,5,4,3,2,1];

numberArray.sort(function(a,b){return b-a})

4. Output today's date in YYYY-MM-DD format

var d = new Date();
// Get the year, and getFullYear() returns a 4-digit number
var year = d.getFullYear();
// Get the month. The month is special. 0 represents January and 11 represents December
var month = d.getMonth() + 1;
// Become two
month = month < 10 ? '0' + month : month;
// Acquisition day
var day = d.getDate();
day = day < 10 ? '0' + day : day;
alert(year + '-' + month + '-' + day);

5. Use regular expressions to replace {id} in the string "< tr > < td > {id} < / td > < td > {name} < / td > < / TR >" with 10 and {name} with Tony

"<tr><td>{$id}</td><td>{$id}_{$name}</td></tr>".replace(/{\$id}/g,'10').replace(/{\$name} /g,'Tony');

         The form of direct quantity in the regular watchband is: / string to match /, / ^ strict Pattern $/, / g by default, all elements matching the string are found globally, replace the matched string, and escape the special symbol with \.

6. In order to ensure the safety of page output, we often need to escape some special characters. Please write a function escapeHtml to escape <, >, & "and"

function escapeHtml(str) {
return str.replace(/[<>"&]/g, function(match) {
    switch (match) {
      case "<":
        return "&lt;";
      case ">":
        return "&gt;";
      case "&":
        return "&amp;";
      case "\"":
        return "&quot;";
      }
  });
}

7. Foo = foo|bar, what does this line of code mean? Why is it written like this?

        If foo exists, the value is foo, otherwise assign the value of bar to foo.

         Short circuit expressions: as operand expressions of "& &" and "|" operators, when evaluating these expressions, the evaluation process will terminate as long as the final result can be determined to be true or false. This is called short circuit evaluation.

8. The output result of the following code is?

var foo = 1;
(function(){
    console.log(foo);
    var foo = 2;
    console.log(foo);
})()
//After the variable declaration is promoted, the above code is equivalent to:
var foo = 1;
(function(){
    var foo;
    console.log(foo); //undefined
    foo = 2;
    console.log(foo); // 2;   
})()

         The function declaration and variable declaration will be implicitly promoted to the top of the current scope by the JavaScript engine, but only the variable name will be promoted without mentioning the appreciation. The code outputs undefined and 2.

9. Use js to randomly select 10 numbers between 10 – 100, store them in an array, and sort them

function randomNub(aArray, len, min, max) {
          if (len >= (max - min)) {
              return 'exceed' + min + '-' + max + 'Number range between' + (max - min - 1) + 'Total number of';
          }
          if (aArray.length >= len) {
              aArray.sort(function(a, b) {
                  return a - b
              });
              return aArray;
          }
          var nowNub = parseInt(Math.random() * (max - min - 1)) + (min + 1);
          for (var j = 0; j < aArray.length; j++) {
              if (nowNub == aArray[j]) {
                  randomNub(aArray, len, min, max);
                  return;
              }
          }
          aArray.push(nowNub);
          randomNub(aArray, len, min, max);
          return aArray;
      }
var arr=[];
randomNub(arr,10,10,100);

10. Merge the two arrays and delete the second element

var array1 = ['a','b','c'];
var bArray = ['d','e','f'];
var cArray = array1.concat(bArray);
cArray.splice(1,1);   //Starting from the (parameter 1) index value of the array, delete (parameter 2) array elements and return the remaining array containing array values
slice() 

Common array API:

toString( )     Convert array to string
indexOf( )     Detect whether the array contains an element. The index of the element is returned. If it is not found, it returns - 1
join( )  Converts an array to a string, specifying the split symbol
concat         Splice multiple arrays and return a large array
reverse( )  Flip array elements
slice( )          Intercept the slice (start, end) of the element in the array; Start is the start subscript and end is the end subscript
splice( )        Delete elements in array
push( )         Add one or more elements to the end of the array and return the elements of the array. The original array will change
pop( )             Delete an element at the end of the array and return the deleted element. The original array will change
unshift( )       Add one or more elements to the beginning of the array and return the elements of the array. The original array will change
shift( )            Delete an element at the beginning of the array and return the deleted element. The original array will change

Posted by fuzzy1 on Fri, 22 Oct 2021 16:55:53 -0700