preface
September is coming soon, the annual golden 9 silver 10 transfer of programmers is about to start, for this, I have never written interview related, also try to sort out a few interview questions for everyone to taste, hope to use on. It’s about understanding
Problem 1: Swap the values of two variables
Interviewer: How do you swap the values of the two variables as many times as possible without resorting to third-party variables and ES6 deconstruction
The code is as follows:
let a = 1
let b = 2
// How do you do it
// ...
console.log(a)/ / 2
console.log(b)/ / 1
Copy the code
Answer key 1:
let a = 1
let b = 2
// How do you do it
a = [b, (b = a)][0]
console.log(a)/ / 2
console.log(b)/ / 1
Copy the code
Answer key 2:
let a = 1
let b = 2
// How do you do it
a = a + b
b = a - b
a = a - b
/ / = >
// a = a + b = 1 + 2 = 3
// b = a - b = 3 - 2 = 1
// a = a - b = 3 - 1 = 2
console.log(a)/ / 2
console.log(b)/ / 1
Copy the code
Answer key 3:
let a = 1
let b = 2
// How do you do it
b ^= a
a ^= b
b ^= a
console.log(a)/ / 2
console.log(b)/ / 1
Copy the code
The second problem: the curryization of functions
Interviewer: Please implement an Add function that meets the following requirements
add(1.2.3) ()/ / 6
add(1.2) (3) ()/ / 6
add(1) (2) (3) ()/ / 6
Copy the code
“The add function must return a function with a variable to store the sum of previous arguments, and when the function receives a parameter of 0, it is the actual result of the calculation…”
function add(cahce, ... args) {
// Return a func function
return function func(. funcArgs) {
if (funcArgs.length) {
// if the func function argument is not 0, the accumulative function is performed
cahce = funcArgs.reduce((p, c) = > (p += c), cahce)
// Then return the func function
return func
} else {
// If the func function argument is 0, the final result is returned
return args.reduce((p, c) = > (p += c), cahce)
}
}
}
console.log(add(1.2.3) ())/ / 6
console.log(add(1.2) (3) ())/ / 6
console.log(add(1) (2) (3) ())/ / 6
Copy the code
The interviewer saw you very quick answer this question, suddenly feel that your skeleton exquisite, but greatly, then, is also to achieve an ADD method, meet the following: 6, 6, 6
add(1.2.3) / / 6
add(1.2) (3) / / 6
add(1) (2) (3) / / 6
Copy the code
The interviewer was deep in thought, “Look at the conditions that are met, the common ground is that there are three parameters, a function can not fix, need to use an auxiliary function to complete the line…”
const curry = function (fn, ... args) {
// The number of arguments to the function can be accessed by the length of the function
return args.length >= fn.length ? // If the number of parameters is greater than or equal to the number of parameters (3) of fn:add1, the clearing is performedfn(... args) :(. _args) = >curry(fn, ... _args, ... args)// Otherwise continue to add parameters
}
const add1 = (x, y, z) = > x + y + z
const add = curry(add1)
console.log(add(1.2.3))/ / 6
console.log(add(1.2) (3))/ / 6
console.log(add(1) (2) (3))/ / 6
Copy the code
Problem 3: array value swap
Create a change function that swaps the index of the specified array as follows:
// Give an array
const arr = [1.2.3.4.5.6.7.8.9]
const oldIndex = 7
const newIndex = 1
console.log( change(oldIndex,newIndex,arr) )// [1, 8, 3, 4, 5, 6, 7, 2, 9]
Copy the code
The interviewer pondered, “There must be an arraysplice
Method, at the beginning of learning, old familiar (this problem I will), so…”
const arr = [1.2.3.4.5.6.7.8.9]
const oldIndex = 7
const newIndex = 1
function change(oldIndex, newIndex, arr) {
arr = arr.slice()
arr.splice(oldIndex, 1, arr.splice(newIndex, 1, arr[oldIndex])[0])
return arr
}
console.log(change(oldIndex, newIndex, arr)) // [1, 8, 3, 4, 5, 6, 7, 2, 9]
console.log(arr) // [1, 2, 3, 4, 5, 6, 7, 8, 9]
Copy the code
4
The interviewer with a long hairline said: Please give the following return values
console.log( ' ' instanceof String )
console.log( '123' instanceof String )
console.log( 123 instanceof Number )
Copy the code
The interviewer ponders, “Is there a trick here?”
Question 5:a == 1 && a== 2 && a==3
Can I do fortrue
You do that?
let a = {
val:1.valueOf(){
return this.val++
}
}
console.log(a == 1 && a== 2 && a==3) // true
Copy the code
After the speech
The first time to write about the interview, about the interview question, mainly biased to understand, after understanding the success of absorption into their own is the ultimate goal, finally
conclusion
If you have any better ideas or questions, feel free to leave a comment
Please point out any inaccuracies or errors in the article