Code:
/**
*
**/
bonusCombination (arr, num, fun) {
if (arr.length < num || num > 10) {
return []
}
let variable = ["a", "b", "c", "d", "e", "f", "g", "h", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u"]
let replaceStr = "-sharp$-sharp"
let str = "var arr=arguments[0]; var fun=arguments[1]; var ret=[]; for (var a = 0; a < arr.length; aPP) { " + replaceStr + " } return ret;"
for (let i = 1; i < num; iPP) {
str = str.replace(replaceStr, " for (var " + variable[i] + " =" + variable[i - 1] + "+ 1; " + variable[i] + " < arr.length; " + variable[i] + "PP) { " + replaceStr + " }")
}
let temp = "var temp= [];"
for (let i = 0; i < num; iPP) {
temp += "temp.push(arr[" + variable[i] + "]);"
}
if (fun) {
temp += "ret.push(fun(temp)); "
} else {
temp += "ret.push(temp);"
}
str = str.replace(replaceStr, temp)
return (new Function(str)).apply(null, [arr, fun])
}
figure:
- doesn"t understand why strings are used as variables. Is there any other function of this method?
- Why did you define a variable; in the first place
- Please explain step by step.