Skip to content

字节:修改以下 print 函数,使之输出 0 到 99,或者 99 到 0 #101

Open

Description

@sisterAn

要求:

1、只能修改 setTimeout 到 Math.floor(Math.random() * 1000 的代码

2、不能修改 Math.floor(Math.random() * 1000

3、不能使用全局变量

function print(n){
  setTimeout(() => {
    console.log(n);
  }, Math.floor(Math.random() * 1000));
}
for(var i = 0; i < 100; i++){
  print(i);
}

Activity

  1. 7777sea commented on Sep 1, 2020

    @7777sea
    function print(n){
      setTimeout((() => {
        console.log(n);
      })(), Math.floor(Math.random() * 1000));
    }
    for(var i = 0; i < 100; i++){
      print(i);
    }
  2. nameRoy commented on Sep 1, 2020

    @nameRoy

    function print(n) {
    setTimeout(
    Promise.resolve(n).then((n) => {
    console.log(n);
    }),
    Math.floor(Math.random() * 1000)
    );
    }
    for (var i = 0; i < 100; i++) {
    print(i);
    }

  3. marlboroKay commented on Sep 1, 2020

    @marlboroKay
    function print(n){
      setTimeout(console.log(n), Math.floor(Math.random() * 1000));
    }
    for(var i = 0; i < 100; i++){
      print(i);
    }
    
  4. Jeckhenry commented on Sep 1, 2020

    @Jeckhenry

    function print(n) {
    setTimeout(() => {
    console.log(n);
    }, 0, Math.floor(Math.random() * 1000));
    }
    for (var i = 0; i < 100; i++) {
    print(i);
    }

  5. dggrok commented on Sep 1, 2020

    @dggrok
    // 取个巧 直接注释掉setTimeout,让他不再无序输出 。这里真正在执行print()时传进去的就是当前的i值
    function print(n){
     // setTimeout((() => {
        console.log(n);
        console.log(99-n)
      //})(), Math.floor(Math.random() * 1000));
    }
    for(var i = 0; i < 100; i++){
      print(i);
    }
  6. Cxy56 commented on Sep 1, 2020

    @Cxy56
    // 方法1, 利用setTimeout、setInterval的第三个参数,第三个以后的参数是作为第一个func()的参数传进去。
    function print(n){
      setTimeout(() => {
        console.log(n);
      }, 1, Math.floor(Math.random() * 1000));
    }
    for(var i = 0; i < 100; i++){
      print(i);
    }
    // 方法2:修改settimout第一个函数参数
    function print(n){
      setTimeout((() => {
        console.log(n);
         return () => {}
      }).call(n), Math.floor(Math.random() * 1000));
    }
    for(var i = 0; i < 100; i++){
      print(i);
    }
    
  7. latte03 commented on Sep 1, 2020

    @latte03
    //async await
    function print(n){
      setTimeout(async () => {
        await console.log(n);
      }), Math.floor(Math.random() * 1000);
    }
    for(var i = 0; i < 100; i++){
      print(i);
    }
  8. herbert-hbt commented on Sep 1, 2020

    @herbert-hbt
    function print(n) {
        setTimeout(() => {
            setTimeout(() => {
                console.log(n)
            }, 1000 * n)
        }, Math.floor(Math.random() * 1000))
    }
    for (var i = 0; i < 100; i++) {
        print(i)
    }
    
  9. ningtiao commented on Sep 1, 2020

    @ningtiao
    function print(n) {
      setTimeout(() => {
        console.log(n)
      }, 0*Math.floor(Math.random() * 1000))
    }
    
    for (var i = 0; i < 100; i++) {
      print(i)
    }
  10. 2604150210 commented on Sep 1, 2020

    @2604150210
    function print (n) {
      setTimeout(async () => {
        await console.log(n)
      }, (1)**Math.floor(Math.random() * 1000) * n);
    }
    
    for(var i = 0; i < 100; i++) {
        print(i)
    }
  11. gu-xiaohui commented on Sep 1, 2020

    @gu-xiaohui
    function print(n) {
      setTimeout(
        (() => {
          console.log(n);
          return function () {};
        })(),
        Math.floor(Math.random() * 1000)
      );
    }
    for (var i = 0; i < 100; i++) {
      print(i);
    }
  12. sisterAn commented on Sep 1, 2020

    @sisterAn
    OwnerAuthor

    总结了一下,解法主要有三种:

    方法一:

    利用 setTimeout 、 setInterval 的第三个参数,第三个以后的参数是作为第一个 func() 的参数传进去

    function print(n){
      setTimeout(() => {
        console.log(n);
      }, 1, Math.floor(Math.random() * 1000));
    }
    for(var i = 0; i < 100; i++){
      print(i);
    }

    方法二:

    修改 setTimeout 第一个函数参数

    function print(n){
      setTimeout((() => {
        console.log(n);
         return () => {}
      }).call(n), Math.floor(Math.random() * 1000));
    }
    for(var i = 0; i < 100; i++){
      print(i);
    }

    方法三:

    利用异步函数

    function print(n){
      setTimeout(async () => {
        await console.log(n);
      }), Math.floor(Math.random() * 1000);
    }
    for(var i = 0; i < 100; i++){
      print(i);
    }
  13. oukatou commented on Nov 7, 2020

    @oukatou

    方法三错的,1000后面少了个括号

  14. nuxio commented on Jan 8, 2021

    @nuxio
    //async await
    function print(n){
      setTimeout(async () => {
        await console.log(n);
      }), Math.floor(Math.random() * 1000);
    }
    for(var i = 0; i < 100; i++){
      print(i);
    }

    这。。写啥 async await哦。。

    function print(n){
      setTimeout(() => {
         console.log(n);
      }), Math.floor(Math.random() * 1000); // 这不是一样?实际是把 Math.floor(Math.random() * 1000); 用 ),  分离了
    }
    for(var i = 0; i < 100; i++){
      print(i);
    }
  15. xiaowuge007 commented on May 12, 2021

    @xiaowuge007
    //多加一个参数不就可以了吗
    function print(n){
      setTimeout(() => {
        console.log(n);
      },1000, Math.floor(Math.random() * 1000));
    }
    for(var i = 0; i < 100; i++){
      print(i);
    } ```
  16. lovezzc commented on Jul 7, 2021

    @lovezzc

    方法一为什么多加一个参数就可以了

  17. xllpiupiu commented on Oct 5, 2021

    @xllpiupiu
    /**
     * setTimeout传参数
     */
    setTimeout((a,b,c)=>{
        console.log(a,b,c)
    },500,'My','name','is xll')
    //解法一 
    function print1(n) {
        setTimeout( () => {
            console.log(n)
        }, 1,Math.floor(Math.random() * 1000))
    }
    for (var i = 0; i < 100; i++) {
        print1(i)
    }
    //解法二  异步函数
    function print2(n) {
        setTimeout(async ()=>{
            await console.log(n)
        }),Math.floor(Math.random()*1000)
    }
    for(var i=0;i<100;i++) {
        print2(i);
    }
  18. NoBey commented on Mar 24, 2022

    @NoBey

    异步函数 那个就是骗人, 不用 async 输出也是对的, 想到与 setTimeout(fn, 0), 宏任务也是按照顺序输出

    其实方法就两种

    1. 破坏 setTimeout 的时间参数, 时期不是随机或者不生效
    2. 还是记录变量 改变 n 的引用, 不在全局变量可以挂在 print 上 print.n++, 或者挂在外部数据 api store 之类的
    function print(n){
      setTimeout(() => {
         console.log(n);
      }), Math.floor(Math.random() * 1000);
    }
  19. CODZR commented on Oct 23, 2024

    @CODZR
    // 方法1, 利用setTimeout、setInterval的第三个参数,第三个以后的参数是作为第一个func()的参数传进去。
    function print(n){
      setTimeout(() => {
        console.log(n);
      }, 1, Math.floor(Math.random() * 1000));
    }
    for(var i = 0; i < 100; i++){
      print(i);
    }
    // 方法2:修改settimout第一个函数参数
    function print(n){
      setTimeout((() => {
        console.log(n);
         return () => {}
      }).call(n), Math.floor(Math.random() * 1000));
    }
    for(var i = 0; i < 100; i++){
      print(i);
    }
    

    借用以上代码

    方法一 让执行的duration全都变成1, 与Math.floor(Math.random() * 1000)无关了
    方法二 .call 立即执行函数直接把console.log执行了,与setTimeout无关了,可以通过在 return () => {} 加上console.log(n)验证

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