directory

  • The introduction

    1. Multiplication (Getting started)
    2. Changing context (getting started)
    3. Changing the properties of objects in batches (getting started)
    4. Property traversal (easy)
    5. Determine whether to include numbers (getting started)
    6. Checking for duplicate strings (easy)
    7. Determine whether to end with a vowel (getting started)
    8. Get the specified string (simple)
    9. Determine if the specified format is true (simple)
    10. Determine whether it conforms to USD format (medium)

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)

51. The multiplication

If a and b are multiplying by each other, it is possible that a and B are decimals. Input: 3, output: 0.0003

// Method 1: Determine the number of decimal places and ensure the accuracy of the number of digits
function multiply(a, b) {
    let arrA = a.toString().split('. ') [1] | |' '
    let arrB = b.toString().split('. ') [1] | |' '
    let fix = arrA.length + arrB.length
    return (a * b).toFixed(fix)
}

// Write it yourself
function multiply(a, b) {
    let arrA = a.toString().split('. ')
    let arrB = b.toString().split('. ')
    let count = 0
    // The first one has a decimal place
    if(arrA[1]) {
        count+=arrA.length
        a = a * Math.pow(10,arrA.length)
    }
    // The second number has a decimal place
    if(arrB[1]) {
        count+=arrB.length
        b = b * Math.pow(10,arrB.length)
    }
    return a * b * Math.pow(10, -count)
}
Copy the code

Related knowledge:

  • Javascript decimal precision problem

Javascript’s numeric store is a 64-bit double precision floating-point number, which, according to IEEE 754, is 0-51 as a value, 52-62 as an exponent, and 63 as a sign bit. Binary floating-point representation has an error for such values as 0.1. The solution is to raise it and then lower it.

52. Changing context

AlterContext (function() {return this.greeting + ‘, ‘+ this.name + ‘! ‘; }, {name: ‘Rebecca’, greeting: ‘Yo’})

// Call
function alterContext(fn, obj) {
    return fn.call(obj)
}

// Apply
function alterContext(fn, obj) {
    return fn.apply(obj)
}

// Bind
function alterContext(fn, obj) {
    return fn.bind(obj)()
}
Copy the code

Related knowledge:

  • call/apply/bind

53. Change the properties of objects in batches

Given a constructor, complete the alterObjects method by pointing the greeting properties of all instances of constructor to the given greeting variable. Input:

var C = function(name) {this.name = name; return this; };var obj1 = new C('Rebecca');   
alterObjects(C, 'What\'s up'); obj1.greeting;  
Copy the code

Output: What’s up

// Method one: Add attributes to the prototype object
function alterObjects(constructor, greeting) {
    constructor.prototype.greeting = greeting
}

Copy the code

Related knowledge:

  • Prototype chain

54. Attribute traversal

Find properties of the object obj that are not on the prototype chain (note that the colon is followed by a space ~)

var C = function() {this.foo = 'bar'; this.baz = 'bim'; }; C.prototype.bop ='bip'; 
iterate(new C());
Copy the code

Output [“foo: bar”, “baz: bim”]

// Method 1: forEach is better than map
function iterate(obj) {
    let arr = []
    Object.getOwnPropertyNames(obj).forEach(value= > arr.push(`${value}: ${obj[value]}`))
    return arr
}
Copy the code

Related knowledge:

  • Object. GetOwnPropertyNames (obj), get all the attributes of the Object itself

55. Determine whether a number is included

Given a string STR, check if it contains a number. Return true if it does, and false otherwise. Type: ‘abc123’; Output: true,

// Method 1: Compare strings with ASCII characters
function containsNumber(str) {
    for (let i = 0; i < str.length; i++) {
        if(str[i] >= '0' && str[i] <= '9') {
            return true}}return false
}

// Implicit type conversion
function containsNumber(str) {
    for (let i = 0; i < str.length; i++) {
        if(str[i] >= 0 || str[i] <= 9) {
            return true}}return false
}

// Regex
function containsNumber(str) {
    return /\d/g.test(str)
}
Copy the code

Related knowledge:

  • The string compares the ASCII code

String comparisons compare their ASCII values. Numbers are consecutive ASCII values, and 0123456789 is 48-57, within this range.

  • Implicit type conversion

Numeric strings and numeric comparisons are implicitly converted to numbers, string and numeric comparisons are converted to NaN, and comparison with numbers returns false, so this is also a way to find out if there are numbers in the string.

  • regular

56. Checking for duplicate strings

Given a string STR, check to see if it contains consecutive repeated letters (a-za-z). Return true if it does, false otherwise. Input: ‘rattler’ Output: true

// Double pointer
function containsRepeatingLetter(str) {
    // Return false if the string length is less than 2
    if(str.length < 2)return false
    // Left and right Pointers
    let left = 0
    let right = 1
    while(str[right]) {
        // Return true if left and right are equal and both are in the a-z range
        if(str[left++] === str[right++] && str[left].toLowerCase() >= 'a' && str[left].toLowerCase() <= 'z' && str[right].toLowerCase() >= 'a' && str[right].toLowerCase() <= 'z') {return true}}// Otherwise return false
    return false
    
}

// Method 2: re, parentheses denote grouping, \1 denote referencing the first grouping
function containsRepeatingLetter(str) {
    return /([a-zA-Z])\1/.test(str);
}

Copy the code

Related knowledge:

  • Double pointer (algorithmic thinking)
  • regular

57. Determine if it ends with a vowel

1. Vowels include a, e, I, O, u, and the corresponding uppercase letters. 2. Output: true,

// Method 1: indexOf
function endsWithVowel(str) {
    return ['a'.'e'.'i'.'o'.'u'].indexOf(str.slice(-1).toLowerCase()) === -1 ? false: true
}

// Method 2: includes
function endsWithVowel(str) {
    return ['a'.'e'.'i'.'o'.'u'].includes(str.slice(-1).toLowerCase())
}
Copy the code

Related knowledge:

  • String methods slice, toLowerCase
  • Array methods indexOf, includes

58. Get the specified string

Given the string STR, check if it contains three consecutive digits 1. If it does, return the string with the first three digits 2. If it does not, return false

PS: Three consecutive increasing numbers.

// Count
function captureThreeNumbers(str) {
  let start = 0
  let count = 0
  for(let i = 0; i < str.length; i++) {
    if (!isNaN(str[i])) {
      count++
    } else {
      count = 0
      start = i+1}}return count >= 3 ? str[start]+str[start+1]+str[start+2] : false
}

// Select match to get the result of the match
function captureThreeNumbers(str) {
  let arr = str.match(/\d{3}/)
  return arr ? arr[0] : false
}
Copy the code

If it is a continuously increasing number, implement it like this:

// Double pointer
function captureThreeNumbers(str) {
    let left = 0
    let right = 0
    while(str[right+1]! = =undefined) {
        // Make sure that the right pointer is consecutive and numeric, and the right pointer moves forward
        if(Math.abs(str[right+1] - str[right]) === 1 && str[right] >=0 && str[right] <=9) {
            right++
        // If less than 3, the left pointer goes to the right
        }else if(right - left + 1 < 3){
            left = ++right
        // Return if the value is greater than 3
        }else {
            return str.substr(left,3)}}// Loop the result, return if greater than 3, otherwise return false
    return right - left + 1> =3 ? str.substr(left,3) : false
}
Copy the code

Related knowledge:

  • counter
  • regular
  • Double pointer (algorithmic thinking) + judge continuous numbers

59. Checks whether the specified format is met

Given the string STR, check whether it is in the following format: 1, XXx-XXX-xxxx 2, where X is Number type: ‘800-555-1212’ Output: true

// Regex
function matchesPattern(str) {
    return /^\d{3}-\d{3}-\d{4}$/.test(str)
}
Copy the code

Related knowledge:

  • Match or decide which format to give priority to re

60. Determine whether it conforms to USD format

If the string is a decimal, the length of the decimal part is 2. If the string is a decimal, the length of the decimal part is 2. $1,023,032.03 or $2.03 in the wrong format: $3,432,12.12 or $34,344.3 Input: ‘$20,933,209.93’ Output: true

// Method 1: re, write from left to right, match in sequence
function isUSD(str) {
    return / ^ \ $] [\ d {1, 3} (\ [d \] {3}) * (. | \ [d \] {2}) $/.test(str)
}
Copy the code

Related knowledge:

  • regular

Already after all pull, 60 problem finish matter, congratulations you, clearance ~~~~