skip to Main Content

I have code

function clearPrintNum(sec, minute) {
    setTimeout(function() {console.clear(); console.log(`${minute} : ${sec}`);}, sec * 1000);
}

for(var minute = 0; minute < 60; minute++){
    for (let second = 0; second < 60; second++) {
        clearPrintNum(second, minute);
    }
}

Which should return first minute then character ":" and after that seconds.
Problem is that variable minute starts at 59, instead of 0.

I tried using while

function clearPrintNum(sec, minute) {
    setTimeout(function() {console.clear(); console.log(`${minute} : ${sec}`);}, sec * 1000);
}

let mins = 0;
while(mins < 60) {
    for (let second = 0; second < 60; second++) {
        clearPrintNum(second, mins);
    }
    ++mins;
}

and

function clearPrintNum(sec, minute) {
    setTimeout(function() {console.clear(); console.log(`${minute} : ${sec}`);}, sec * 1000);
}

var minute = 0;
for(; minute < 60; minute++){
    for (let second = 0; second < 60; second++) {
        clearPrintNum(second, minute);
    }
}

but it doesn’t work.

2

Answers


  1. The loop runs very quickly, which is why you only see 59.
    If you remove console.clear();, you will see that the values start at 0.

    function clearPrintNum(sec, minute) {
        setTimeout(function() {
          // console.clear(); 
          console.log(`${minute} : ${sec}`);
          }, 
        sec * 1000);
    }
    
    for(var minute = 0; minute < 60; minute++){
        for (let second = 0; second < 60; second++) {
            clearPrintNum(second, minute);
        }
    }
    
    

    Your output will look like this:

    0 : 0
    1 : 0
    2 : 0
    3 : 0
    4 : 0
    5 : 0
    6 : 0
    7 : 0
    8 : 0
    9 : 0
    10 : 0
    ...
    
    Login or Signup to reply.
  2. Problem is that variable minute starts at 59, instead of 0.

    No, it starts at 0. You’ve omitted useful debugging information in your use of console.clear(). Remove that and you’ll see that every second you log 60 values, from 0 to 59:

    function clearPrintNum(sec, minute) {
        setTimeout(function() {
          console.log(`${minute} : ${sec}`);
        }, sec * 1000);
    }
    
    for(var minute = 0; minute < 60; minute++){
        for (let second = 0; second < 60; second++) {
            clearPrintNum(second, minute);
        }
    }

    Which essentially demonstrates that every second you’re logging that second’s value for every minute, all at once.

    This is because the timeout only accounts for seconds:

    sec * 1000
    

    If you want to account for minutes as well, add that to the calculation:

    sec * 1000 + minute * 60000
    

    For example:

    function clearPrintNum(sec, minute) {
        setTimeout(function() {
          console.clear();
          console.log(`${minute} : ${sec}`);
        }, sec * 1000 + minute * 60000);
    }
    
    for(var minute = 0; minute < 60; minute++){
        for (let second = 0; second < 60; second++) {
            clearPrintNum(second, minute);
        }
    }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search