Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to properly add setInterval/setTimeout to my function

Pls, help!

The function should print the letters "A" through "F" to the console, one letter per second.

The function works without setInterval/setTimeout.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

// setInterval(alphabet, 1000, 'a', 'f');

function alphabet(from, to) {
  var a = [], i = from.charCodeAt(0), j = to.charCodeAt(0);
  for (; i <= j; ++i) {
    setInterval(() => {
      a.push(String.fromCharCode(i));
    }, 1000);
  }
  // return console.log(a);
  return a;
}

console.log(alphabet('a', 'f'));

>Solution :

You might wanna try this.
You can paste it on codepen

var a = [];
function alphabet(from, to) {
    console.log(a);
    setInterval(() => {
        var i = from.charCodeAt(0);
        var j = to.charCodeAt(0);
        for (; i <= j; ++i) {
          if(a.includes(String.fromCharCode(i))){
             
          }else{
            console.log(String.fromCharCode(i))
            a.push(String.fromCharCode(i));
            break;
          }
        }
    }, 1000);
  return a;
}

alphabet('a', 'f');
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading