The interview questions

Remember a question I asked in the interview: I don’t remember the whole question, only the general requirements

  1. Basic problem: write a method: pass parameter number, if pass parameter 3, generate similar to the following table
1 1 1
2 2 2
3 3 3

For parameter 4, the following table is generated

1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4

And so on

Here’s my idea:

  • numTo pass the parameter, initialize a value of lengthnumThe array is filled with array elements1; Initialize an array of length:new Array(length), array fills the value.fill();
  • Iterate through the array, initializing each element with lengthnumAnd populate the arrayThe index value + 1; Used heremapTraversal method,mapNeed to bereturn
function arr(num){
    return new Array(num).fill(1).map((item,i) = >{
        return new Array(num).fill(i+1);
   });
}
Copy the code

The running results are as follows:

2. Extend the topicOn the basis of 1, the content of the table generated under transformation is as follows:

1 2 3
6 5 4
7 8 9
1 2 3 4
8 7 6 5
9 10 11 12
16 15 14 13

So here’s my way of thinking:

  • Initialize an array of num length based on 1, and fill the array with 1

  • Select * from map where num = 1 and the index *num + 1 is set to 1. Otherwise, the new Array(num) will be empty. The traverser cannot traverse and fetch index and item, as shown in the following figure

  • The two-dimensional array is mapped again, removing the first value and adding +1 to the remaining values

  • To judge the odd and even lines, count from 0, and reverse() is required for the odd and even lines, and mod 2 is required for the odd and even lines

  • return arry; Return the concatenated array

function arr(num){
    let array = new Array(num).fill(1).map((item,i) = >{
        let arrItem = new Array(num).fill(1).fill(i*num + 1.0 ,1);
        let res = arrItem.map((val, key) = > { return key === 0 ? val : key+arrItem[0]; });return i%2= = =1 ? res.reverse() : res;
        
   }); 
   return array;
}
Copy the code

The running results are as follows:

reflection

After writing the test, I interviewed the existing problem in the console. I used the wrong forEach and map and the wrong reduce method

The foundation is not solid enough, so take this opportunity to consolidate the array string cutting and other methods ~

maptraverse

Does not change the original array, returns a new array

forEachtraverse

Used to operate on the elements of an array without changing the value of the original array and return undefined

filterFilter filter array

Filter is used to filter the values that match the criteria, and returns a new array without modifying the original array

reduceSummation of arrays

Common usage is as follows:

  1. No extra parameters:reduce((sum,item,index)=>{return sum+item})This method represents the values from an array1.(The index valueindexIs 1) start the cycle,sumThe initial value is the value of the array subscript 0
  2. Pass the initial value:reduce((sum,item,index)=>{return sum+item}, initData)The method is represented from an arrayThe zeroth(The index valueindexIs 0) starts the loop,sumThe initial value for theinitData

fillMethod to batch fill an array with values

Fill is used to initialize an array and is commonly used in the following ways

  1. array.fill(data): Passes only one parameter, which assigns a value to each element of the arraydata
  2. array.fill(data, startIndex, endIndex): indicates that the array index value isstartIndextoendIndexBetweendata
  3. array.fill(data, startIndex): represents an array fromstartIndexThe index value starts to be assigneddata

splicemethods

Array. Splice (index, howmany, item1… ItemX) deletes or adds elements to the original array, if deleted, returns the array composed of deleted elements

  1. Arr. Splice (index 0, item1... itemX)Says from theindexPost-index insertThe item... itemXdata

  1. arr.splice(index, howmany)Says from theindexIndex started to drophowmanyValue that returns an array of deleted elements

  1. Arr. Splice (index, howmany, item1... itemX)Says from theindexIndex started to drophowmanyAnd insertThe item... itemXData that returns an array of deleted elements

splitmethods

Str.split (), which specifies the delimiter to split the string into arrays. The delimiter can be a string or a re

slicemethods

Slice (start[, end]), where start end is a positive or negative integer, is used to slice the string without changing the original string

substring

Substring (start[, end]) and start end are both non-negative integers that are used to cut the string without changing the original string

substr

Substr (start[, length]), start can be negative, indicating the penultimate number, used to cut the string without changing the original string

conclusion

1. A method that changes an array or string

  • splice: Adds or deletes the original array and returns the array of deleted elements
  • fill: prefilled value of the original array

2, does not change the original array or string method

  • slice(start[, end]): returns the cut string,startandendCan be a negative
  • The substring (start [end]): returns the cut string,startandendCan’t be negative
  • substr(start[, length]): returns the cut string,startCan be a negative
  • split(): Specifies delimiters to be split into arrays. Delimiters can be strings or re’s
  • filter: filter that returns the array that meets the requirement
  • forEach: Iterates through the array, performing an operation on each element, and returns the valueundefined
  • reduce: Summation method
  • map: Returns a new array that operates on the original array

3, remember some other common methods

  • findIndex: Returns the first index that meets the requirement
  • find: returns the first one that meets the requirementitem
  • some: Returns a value that satisfies the conditiontrueOtherwise returnfalse
  • every: Returns if all values meet the conditiontrueOtherwise returnfalse
  • includes: Returns if the array contains an itemtrueOtherwise returnfalse