Skip to content

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

Open

Description

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

Activity

  1. CodeRookie262 commented on Dec 31, 2020

    @CodeRookie262
    OwnerAuthor

    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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions