Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

写出或者简短描述函数调用:fun(2,3,4) 的输出结果。 #12

Open
CodeRookie262 opened this issue Dec 30, 2020 · 1 comment

Comments

@CodeRookie262
Copy link
Owner

CodeRookie262 commented Dec 30, 2020

function fun(...dimentions){
	const [layer,...d] = dimentions;
	return d.length ? Array(layer).fill(0).map(_ => fun(...d)) : Array(layer)
}
@CodeRookie262
Copy link
Owner Author

const [layer,...d] = dimentions; 属于结构操作,从代码中可以看出来如果函数参数个数大于 1 个时就会进行递归操作,每次调用都会生成一个数组,长度为函数的第一个参数 layer,并且每次递归都会生成一个维度的数组,维度的大小有函数参数的个数觉得,每一维度中数组的长度由当前函数调用的第一个参数决定,当函数参数只有一个时终止递归调用并且生成一个长度为 layer 的数组;

fun(2) // = Array(2)
fun(3) // = Array(3)
fun(4) // = Array(4)

fun(2,3) // Array( Array(3),Array(3) )
fun(2,3,4) // Array( Array( Array(4),Array(4),Array(4) ) , Array( Array(4),Array(4),Array(4) ) )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant