So i've been working on this title case project for a bit. I was working out some bugs to handle instances of when a user has already submitted a title and they want to submit another one. I need to somehow either clear the screen AND run grabText function if it is not the user's first submission (meaning there is already the converted title on screen). So if there is a converted title on screen then i need to clear it off the screen and run grabText functions on the second submission the user is trying to convert. I hope I explained that clear enough. I tried doing an if else statement earlier to check and and see if the converted title appeared in the DOM. If it did appear, it removed it but then i didn't know how to continue on to have it run the last else statement (which basically converts the text and displays it). I think i am overthinking this. Left out some code relating to copying to clipboard as it is not necessary. Project is hosted here: https://dmking0728.github.io/title-case-converter/#

const button = document.querySelector('.button');
const copy = document.querySelector('#copy');

//event listeners
button.addEventListener('click', grabText);
copy.addEventListener('click', copyTo);


function grabText() {
  const textBox = document.querySelector('#text-box').value;
  const splitStr = textBox.toLowerCase().split(" ");
  const exceptions = ["and", "the", "a", "an", "for", "to","but", "at","by", "on", "as", "in", "of", "so", "by", "nor", "or", "up", "yet"]

  if(textBox == "") {
    alert('Enter a title!')
  } else {
    for(i = 0; i < splitStr.length; i++) {
      //if it is not the 1st item in the splitStr array (the first word) and it is found in the exception array, then we skip running the capitlization code that follows (it will be lowercase)
      if(i > 0 && exceptions.includes(splitStr[i]))
      //if it is found in exception array skip capitilization 
      continue;

      splitStr[i] = splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1);
    }

0 Answers