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

练习题2:不使用数组内置 API 实现等差数组 #13

Open
chenhuiYj opened this issue Jun 17, 2020 · 0 comments
Open

练习题2:不使用数组内置 API 实现等差数组 #13

chenhuiYj opened this issue Jun 17, 2020 · 0 comments

Comments

@chenhuiYj
Copy link
Owner

不使用 for, while 以及 所有数组内置的 API ,实现将一个空数组赋值成 [0,2,4,6,8....100]

实现方式

function handle(){
    let arr=[]
    let item=0
    function fn(){
        arr[item]=item*2
        item+=1
        if(item<=50){
            fn()
        }
    }
    fn()
    return arr
}
handle()

把代码贴到 console 就可以正常的输出了,但是如果下次如果要求输出 [0,2,4....200]呢?这个很好解决,只要加个参数 num 就可以实现。规定循环多少次才输出

function handle(num){
    let arr=[]
    let item=0
    function fn(){
        arr[item]=item*2
        item+=1
        if(item<=num){
            fn()
        }
    }
    fn()
    return arr
}
handle(50)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant