Typing and deleting letters of a word in the JS

Now that I look at the code again we can get rid of convertArray altogether and just use the typeText array instead. That saves another 2 lines of code.

function textLoop() {
    myElement.innerHTML = typeText[counter].split("").slice(0, pos).join("");
    pos += increment;
    if (pos === typeText[counter].length) {
      increment = -increment;
    }
    if (pos === 0) {
      increment = 1;
      counter++;
      if (counter === typeText.length) {
        counter = 0;
      }
    }
    timeLoop = setTimeout(textLoop, 100);
  }
  textLoop();

Whether @Paul_Wilkins will think this line is too complicated is another matter :slight_smile:

myElement.innerHTML = typeText[counter].split("").slice(0, pos).join("");

1 Like