I met an interview question today. I found a problem when using js to implement the algorithm. How to use js to implement such a method. Given a function and nesting depth (int type), return a nested structure as shown below?
Given parameters: fn, deepLength
Return structure (take deepLength=3 as an example):
for (var x = 0; x <= 9; x++) {
for (var y = 0; y <= 9; y++) {
for (var z = 0; z <= 9; z++) {
rs = fn([x,y,z])
}
}
}
After thinking about it for a while, it seems that it can only be achieved through nested functions. Is there a more elegant implementation method?
I summarized everyone’s methods and put them on a demo page for your reference
Attached link Interview question: Dividing cookies
Implemented by building strings, but it feels too inelegant. .
Interview question reference code