JavaScript-Algorithms icon indicating copy to clipboard operation
JavaScript-Algorithms copied to clipboard

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

Open sisterAn opened this issue 5 years ago • 19 comments

要求:

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

sisterAn avatar Aug 31 '20 15:08 sisterAn

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

7777sea avatar Sep 01 '20 01:09 7777sea

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

nameRoy avatar Sep 01 '20 01:09 nameRoy

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

marlboroKay avatar Sep 01 '20 01:09 marlboroKay

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

Jeckhenry avatar Sep 01 '20 01:09 Jeckhenry

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

dggrok avatar Sep 01 '20 01:09 dggrok

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

Cxy56 avatar Sep 01 '20 01:09 Cxy56

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

latte03 avatar Sep 01 '20 01:09 latte03

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

herbert-hbt avatar Sep 01 '20 09:09 herbert-hbt

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

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

ningtiao avatar Sep 01 '20 09:09 ningtiao

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

2604150210 avatar Sep 01 '20 10:09 2604150210

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

gu-xiaohui avatar Sep 01 '20 10:09 gu-xiaohui

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

方法一:

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

sisterAn avatar Sep 01 '20 14:09 sisterAn

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

oukatou avatar Nov 07 '20 06:11 oukatou

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

nuxio avatar Jan 08 '21 09:01 nuxio

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

xiaowuge007 avatar May 12 '21 07:05 xiaowuge007

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

lovezzc avatar Jul 07 '21 14:07 lovezzc

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

xllpiupiu avatar Oct 05 '21 07:10 xllpiupiu

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

NoBey avatar Mar 24 '22 08:03 NoBey