directory
-
The introduction
- Using Arguments (Getting Started)
- Calling functions with Apply (easy)
- Quadratic wrapping function (simple)
- Corrification (Introduction)
- Or operations (getting started)
- And operation (getting started)
- Modules (more difficult)
- Binary conversion (easy)
- Binary Conversion (Getting started)
- Binary conversion (easy)
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)
41. Using the arguments
The new function useArguments can accept 1 or more parameters. Implement the function useArguments that returns the sum of all the call parameters. In this case, all test parameters are of the Number type and parameter conversion is not required. Input: 1, 2, 3, 4 Output: 10
// Method 1: direct traversal
function useArguments() {
let count = 0
for(let i = 0; i < arguments.length; i++) {
count+= arguments[i]
}
return count
}
// Select * from arguments; // Select * from arguments
function useArguments() {
return Array.from(arguments).reduce((prev, item) = > prev + item, 0)}Copy the code
Related knowledge:
- Arguments – Fetching function arguments and class array operations.
42. Use Apply to call the function
After the callIt function is called, the following conditions are met: 1. The return result is the result after the call to fn. 2
// Method 1: truncate the argument from the first one and call apply
function callIt(fn) {
let newArr = Array.from(arguments).slice(1)
return fn.apply(this,newArr)
}
Copy the code
Related knowledge:
- apply/arguments
43. Quadratic encapsulation function
The partialUsingArguments function is called with the following conditions: Fn (partialUsingArguments); fn (partalusingarguments); fn (partalusingarguments); fn (partalusingarguments) None Output: None
// Call extends the operator directly
function partialUsingArguments(fn) {
let args = [].slice.call(arguments.1)
return function result(. newArgs) {
return fn.call(this. args,... newArgs) } }// Use apply to receive arrays
function partialUsingArguments(fn) {
let args = [].slice.call(arguments.1)
return function result(. newArgs) {
return fn.apply(this,args.concat(newArgs))
}
}
// Bind
function partialUsingArguments(fn) {
let args = [].slice.call(arguments.1)
return function result(. newArgs) {
return fn.bind(this. args,... newArgs)() } }Copy the code
Related knowledge:
- arguments/apply/call/bind
44. Currie,
It is known that fn is a predefined function that implements curryIt. After calling fn, the following conditions are met: Return a function a whose length is 1 (that is, explicitly declare a to receive a parameter). Return a function c whose length property is 1. Var fn = function (a, b, c) {return a + b + c}; curryIt(fn)(1)(2)(3); The output of 6
function curryIt(fn) {
let len = fn.length
let args = []
return function (arg){
args.push(arg)
if(len > args.length) {
return arguments.callee
}
return fn.apply(this, args)
}
}
Copy the code
Related knowledge:
- Currie,
Functional programming (iii) – Coriolization
- Function parameter length: fn.length
45. Or operation
Return the logical or operational results of arguments A and b. Enter false, true; Output: true,
// Method one: direct or operator
function or(a, b) {
return a || b
}
// Method 2: the or operation in the bit operation, which returns 0 or 1, needs to be converted to a Boolean value
function or(a, b) {
return a | b ? true : false
}
Copy the code
Related knowledge:
- Or operation
| | operators, a and b are false, are false, one is true, is false | operator, is a calculation, the position is only returns 0 0, position has a 1 is returned, if return need a Boolean value, need to translate
46. And operation
Return the logic of arguments A and b with the result of the operation input: false, true output: false
// Method one: and operator, return a Boolean value
function and(a, b) {
return a && b
}
// The bitwise and operator returns 0 and 1, which need to be converted to a Boolean value
function and(a, b) {
return a & b ? true : false
}
Copy the code
Related knowledge:
- And operation
The false & operator is a bit operator that returns 1 if both a and b are true. The false & operator is a bit operator that returns 1 if both a and B are true. The false & operator returns 0 if there is a 0
47. The module
When the createModule function is called, the following requirements are met: The sayIt method returns the string greeting + ‘, ‘+ name. The sayIt method returns the string greeting + ‘,’ + name
// Method 1: objects
function createModule(str1, str2) {
return {
greeting: str1,
name: str2,
sayIt: function (){
return this.greeting + ', ' + this.name
}
}
}
// Method two: constructor
function createModule(str1, str2) {
function Obj() {
this.greeting = str1
this.name = str2
}
Obj.prototype.sayIt = function () {
return this.greeting + ', ' + this.name
}
return new Obj
}
Copy the code
Related knowledge:
- Modularity, constructors, objects
48. Binary conversion
Get the first bit of the binary form num. For example: the binary of 2 is 10, the first bit is 0, the second bit is 1 input: 128,8 output: 1
// Method 1: while loop
function valueAtBit(num, bit) {
let arr = []
while(num){
arr.push(num % 2)
num = Math.floor(num / 2)}return arr[bit-1]}// Method 2: move the bit right >> bit 1 and then take modulo 2
function valueAtBit(num, bit) {
return (num >> bit - 1) % 2
}
Copy the code
Related knowledge:
- Binary numerical operation
- modulus
- Bitwise operators move left >> right << unsigned bits move right <<<
49. Binary conversion
Given a binary string, convert it to the corresponding decimal number input: ‘11000000’ output: 192
// parseInt(STR,2)
// Method 2:
function base10(str) {
let count = 0
for(let i = 0; i < str.length; i++) {
count = count * 2 + str[i]*1
}
return count
}
Copy the code
Related knowledge:
- Hexadecimal conversion
From binary to decimal, multiply by 2 from the highest bit. From decimal to binary, mod by 2 and reverse
50. Binary conversion
The problem describes converting a given number to a binary string. If the string length is less than 8 bits, 0 to the full 8 bits are added. Input: 65 Output: 01000001(string)
// Method 1: check bits after conversion (algorithmic thinking, recommended)
function convertToBinary(num) {
let arr = []
while(num) {
arr.push(num % 2)
num = num >> 1
}
let str = ' '
let len = arr.length > 8 ? arr.length : 8
for(let i = len - 1; i >= 0; i--){
str = arr[i] ? str + '1' : str + '0'
}
return str
}
// Method 2: use API to convert to binary and see missing (try not to add strings in front of it, performance consumption is high)
function convertToBinary(num) {
var str = num.toString(2);
while(str.length < 8) {
str = "0" + str;
}
return str;
}
Copy the code
Related knowledge:
- Binary conversion
There is a victory, it is really by shallow to difficult depth ~~~~