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

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

Open
sisterAn opened this issue Aug 31, 2020 · 18 comments

Comments

@sisterAn
Copy link
Owner

要求:

1、只能修改 setTimeoutMath.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);
}
@7777sea
Copy link

7777sea commented Sep 1, 2020

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

@nameRoy
Copy link

nameRoy commented Sep 1, 2020

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

@marlboroKay
Copy link

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

@Jeckhenry
Copy link

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

@dggrok
Copy link

dggrok commented Sep 1, 2020

// 取个巧 直接注释掉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);
}

@Cxy56
Copy link

Cxy56 commented Sep 1, 2020

// 方法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);
}

@latte03
Copy link

latte03 commented Sep 1, 2020

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

@herbert-hbt
Copy link

herbert-hbt commented Sep 1, 2020

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

@ningtiao
Copy link

ningtiao commented Sep 1, 2020

function print(n) {
  setTimeout(() => {
    console.log(n)
  }, 0*Math.floor(Math.random() * 1000))
}

for (var i = 0; i < 100; i++) {
  print(i)
}

@2604150210
Copy link

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

@gu-xiaohui
Copy link

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

@sisterAn
Copy link
Owner Author

sisterAn commented Sep 1, 2020

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

方法一:

利用 setTimeoutsetInterval 的第三个参数,第三个以后的参数是作为第一个 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);
}

@oukatou
Copy link

oukatou commented Nov 7, 2020

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

@nuxio
Copy link

nuxio commented Jan 8, 2021

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

@xiaowuge007
Copy link

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

@lovezzc
Copy link

lovezzc commented Jul 7, 2021

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

@xllpiupiu
Copy link

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

@NoBey
Copy link

NoBey commented Mar 24, 2022

异步函数 那个就是骗人, 不用 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);
}

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