GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — 字节:修改以下 print 函数,使之输出 0 到 99,或者 99 到 0 · Issue #101 · sisterAn/JavaScript-Algorithms

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

New issue
Copy link
New issue
Copy link
Open
Open
字节:修改以下 print 函数,使之输出 0 到 99,或者 99 到 0#101
Copy link
Labels
字节编程题

Description

@sisterAn
sisterAn
opened on Aug 31, 2020
Issue body actions

要求:

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. sisterAn
    added
    字节
    编程题
    on Aug 31, 2020
  2. 7777sea commented on Sep 1, 2020

    @7777sea
    7777sea
    on Sep 1, 2020
    More actions
    function print(n){
      setTimeout((() => {
        console.log(n);
      })(), Math.floor(Math.random() * 1000));
    }
    for(var i = 0; i < 100; i++){
      print(i);
    }
    Reacted by Cxy56, 小灰, 草苺奶昔LMT, hayeslv, ipsum-0320, vaakian and gitgitMu
  3. nameRoy commented on Sep 1, 2020

    @nameRoy
    nameRoy
    on Sep 1, 2020
    More actions

    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);
    }

    Reacted by Cxy56 and gitgitMu
  4. marlboroKay commented on Sep 1, 2020

    @marlboroKay
    marlboroKay
    on Sep 1, 2020
    More actions
    function print(n){
      setTimeout(console.log(n), Math.floor(Math.random() * 1000));
    }
    for(var i = 0; i < 100; i++){
      print(i);
    }
    
    Reacted by Edward Cui, Agan and Harmonior
    Reacted by hayeslv, 李宗保 and gitgitMu
  5. Jeckhenry commented on Sep 1, 2020

    @Jeckhenry
    Jeckhenry
    on Sep 1, 2020
    More actions

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

    Reacted by lyvvq9296 and 李宗保
  6. dggrok commented on Sep 1, 2020

    @dggrok
    dggrok
    on Sep 1, 2020
    More actions
    // 取个巧 直接注释掉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);
    }
    Reacted by Cxy56, Leo, mlanfe, ipsum-0320, 李宗保, Ember hale, destinyoung and ShiRin
    Reacted by Cxy56
  7. Cxy56 commented on Sep 1, 2020

    @Cxy56
    Cxy56
    on Sep 1, 2020
    More actions
    // 方法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);
    }
    
    Reacted by Junting and Andrew
  8. latte03 commented on Sep 1, 2020

    @latte03
    latte03
    on Sep 1, 2020
    More actions
    //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);
    }
    Reacted by herbert-hbt, yondmn, Leo, WU HAN, Agan, KylinLee and hyyabc123
    Reacted by 李宗保 and jiangleo
  9. herbert-hbt commented on Sep 1, 2020

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

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

    @2604150210
    2604150210
    on Sep 1, 2020
    More actions
    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)
    }
  12. gu-xiaohui commented on Sep 1, 2020

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

    @sisterAn
    sisterAn
    on Sep 1, 2020
    OwnerAuthor
    More actions

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

    方法一:

    利用 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);
    }
    Reacted by Xulu Chu
    Reacted by vaakian and gitgitMu
  14. oukatou commented on Nov 7, 2020

    @oukatou
    oukatou
    on Nov 7, 2020
    More actions

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

  15. nuxio commented on Jan 8, 2021

    @nuxio
    nuxio
    on Jan 8, 2021
    More actions
    //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);
    }
    Reacted by vaakian
  16. xiaowuge007 commented on May 12, 2021

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

    @lovezzc
    lovezzc
    on Jul 7, 2021
    More actions

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

  18. xllpiupiu commented on Oct 5, 2021

    @xllpiupiu
    xllpiupiu
    on Oct 5, 2021
    More actions
    /**
     * 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);
    }
  19. NoBey commented on Mar 24, 2022

    @NoBey
    NoBey
    on Mar 24, 2022
    More actions

    异步函数 那个就是骗人, 不用 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);
    }
    Reacted by 李宗保
  20. CODZR commented on Oct 23, 2024

    @CODZR
    CODZR
    on Oct 23, 2024
    More actions
    // 方法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

    Labels

    字节编程题

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.