directory

  • The introduction

    1. Delete the last element of an array (getting started)
    2. Adding elements (Getting started)
    3. Delete the first element of an array (getting started)
    4. Array merging (Getting started)
    5. Adding elements (easy)
    6. Counting (Getting started)
    7. Finding duplicate elements (medium)
    8. Quadratic (getting started)
    9. Finding element Locations (getting started)
    10. Avoid global variables (getting started)

The introduction

Niuke.com this front-end written test database, it can be said that the 60 is the most basic, but also inspect things more miscellaneous, have time 4 days almost can brush, consolidate the foundation or some use. Finish the problem review can be done all morning. Now I combine my answers with other people’s answers and make a summary here, which is also the result of sorting out my knowledge.

  • Front-end brush question – niuke.com front-end question bank 60 details (a)
  • Front-end brush question – niuke.com front-end question bank 60 details (two)
  • Front-end brush question – niuke.com front-end question bank 60 details (three)
  • Front-end brush question – niuke.com front-end question bank 60 details (four)
  • Front-end brush question – niuke.com front-end question bank 60 details (five)
  • Front-end brush question – niuke.com front-end question bank 60 details (six)

21. Deletes the last element of the array

Delete the last element of array arr. [1, 2, 3, 4] output: [1, 2, 3]

I won’t write the other easy ways

// The most common method
function truncate(arr) {
    return arr.slice(0, -1)}// Return new array API + pop()
function truncate(arr) {
    let newArr = [].concat(arr)
    newArr.pop()
    return newArr
}
Copy the code

Related knowledge:

  • An array of API

22. Add elements

Add item to array arr. [1, 2, 3, 4], 10 output: [10, 1, 2, 3, 4]

// Method 1: Put the last one in first, then iterate through the group
function prepend(arr, item) {
    let newArr = [item]
    for(let i = 0; i < arr.length ; i++) {
        newArr.push(arr[i])
    }
    return newArr
}

// Copy a new array and add item to the first one
function prepend(arr, item) {
    let arr1 = arr.slice(0)
    arr1.splice(0.0, item) // arr1.unshift(item)
    return arr1
}

// Use concat directly to return a new array
function prepend(arr, item) {
    return [item].concat(arr)
}
Copy the code

Related knowledge:

  • An array of API

23. Deletes the first element of the array

Delete the first element of array arr. [1, 2, 3, 4] output: [2, 3, 4]

// Method 1: for loop push element
function curtail(arr) {
    let newArr = []
    for(let i = 1; i < arr.length ; i++) {
        newArr.push(arr[i])
    }
    return newArr
}

// Copy the original array
function curtail(arr) {
    let newArr = arr.slice(0)
    newArr.shift()
    return newArr
}

// Slice returns a new array
function curtail(arr) {
    return arr.slice(1)}Copy the code

Related knowledge:

  • Array method API

24. An array of merger

Merge array arr1 and array arR2. Don’t directly modify the array arr, as a result, returns a new array input: [1, 2, 3, 4], [‘ a ‘, ‘b’, ‘c’, 1] output: [1, 2, 3, 4, ‘a’, ‘b’, ‘c’, 1)

// Method 1: iteration
function concat(arr1, arr2) {
    var newArr=arr1.slice(0);
    for(var i=0; i<arr2.length; i++){ newArr.push(arr2[i]); }return newArr;
}

// Array merge
function concat(arr1, arr2) {
    return arr1.concat(arr2)
}
Copy the code

Related knowledge:

  • An array of API

25. Add elements

Add item to index of array arr. [1, 2, 3, 4], ‘z’, 2 output: [1, 2, ‘z’, 3, 4]

// Method 1: normal iteration
function insert(arr, item, index) {
    let newArr = []
    for(let i = 0; i < arr.length; i++) {if(i === index) {
            newArr.push(item)
            newArr.push(arr[i])
        } else {
            newArr.push(arr[i])
        }
    }
    return newArr
}

// New array +splice method
function insert(arr, item, index) {
    let newArr = arr.slice(0)
    newArr.splice(index,0,item)
    return newArr
}

// Create an array
function insert(arr, item, index) {
    // Divide into two arrays according to index and concat
    return arr.slice(0,index).concat(item,arr.slice(index))
}
Copy the code

Related knowledge:

  • An array of API

26. count

The number of occurrences of elements in arr whose value is equal to item input: [1, 2, 4, 4, 3, 4], 4 Output: 3

// Count
function count(arr, item) {
    let count = 0
    arr.forEach((value,index) = > {
        if(value === item) count++
    })
    return count
}

// Method 2: filter the array length equal to item
function count(arr, item) {
    let newArr = arr.filter(val= > val === item)
    return newArr.length
}

// Method 2: reduce
function count(arr, item) {
  return arr.reduce((prev, val) = > {
    return val === item ? ++prev : prev
  }, 0)}Copy the code

Related knowledge:

  • An array of API

27. Finding duplicate elements

[1, 2, 4, 4, 3, 3, 1, 5, 3] [1, 3, 4]

// If the two Pointers are adjacent to each other, they are placed in the result array
function duplicates(arr) {
    if(arr.length < 2) return arr
    let newArr = arr.slice(0).sort()
    let result = []
    let i = 0
    let j = 1
    while(j < newArr.length) {
        if(newArr[i] === newArr[j]){
            if(! result.includes(newArr[i])) result.push(newArr[i]) }else {
            i = j
        }
        j++
    }
    return result
}

// Use map to record count and output values greater than 1
function duplicates(arr) {
    let map = new Map()
    arr.forEach(value= > {
        if (map.has(value)) {
          let count = map.get(value)
          map.set(value,++count)
        } else {
          map.set(value,1)}})let arr1 = []
    for(let [key,index] of map) {
      if(map.get(key) > 1) arr1.push(key)
    }
    return arr1
}

// Only one concept is that the first occurrence of the index is different from the second occurrence of the index
function duplicates(arr) {
    let newArr = []
    arr.forEach((value, index) = > {
        if(arr.indexOf(value) ! == arr.lastIndexOf(value) && newArr.indexOf(value) === -1) newArr.push(value)
    })
    return newArr
}
Copy the code

Related knowledge:

  • Algorithm: double pointer
  • counter
  • The index

28. For a quadratic

And they say take each element of the array ARR to the second power. [1, 2, 3, 4] output: [1, 4, 9, 16]

The syntax above ES6 is not supported. ** is not supported, but it can be used normally.

// Method 1: map
function square(arr) {
    // Square can be math.pow (value, 2) or value ** 2
    return arr.map(value= > value * value)
}

// forEach: How to implement map with forEach
function square(arr) {
    let newArr = []
    arr.forEach((value, item) = > {
        newArr.push(value * value)
    })
    return newArr
}
Copy the code

Related knowledge:

  • Array map method, and map method implementation
  • The way you write it squared

29. Find element location

Topic described in array arr, find the value and item equal elements in all position input: [‘ a ‘, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘a’, ‘b’, ‘c’] ‘a’ output: [0, 6]

// forEach, the other traversal is similar, must be traversal.
function findAllOccurrences(arr, target) {
    let newArr = []
    arr.forEach((value,index) = > {
        if(target === value) newArr.push(index)
    })
    return arr1
}
Copy the code

Related knowledge:

  • An array of API

30. Avoid global variables

There are global variables in the given JS code. Please fix them

function globals() {
    myObject = {
      name : 'Jory'
    };

    return myObject;
}
Copy the code
Var /let/const myObject
function globals() {
    const myObject = {
      name : 'Jory'
    };

    return myObject;
}
Copy the code

Related knowledge:

  • scope

The array method is finally done ~~~~