DEV Community

maxazillion
maxazillion

Posted on

Words Per Minute

This project I made to test out how the date time works on java script. One of the issues I ran into during this project was that if I were to do everything in just one function, both date times were instantiated. This means the function would not give an accurate end time. I had to move the date time to be outside of the main function. Of course it could be completely possible to do this cleaner and without moving the data time but this is how I did it with the limited knowledge I have on this.

function getSeconds(){
  let date = new Date()
  let sec = (date.getMinutes() * 60) + date.getSeconds();
  return sec;
}
function wordCount() {
  let words = 1;
  let start = getSeconds();
  let end = 0;
  let timeDiff = 0;
  let wpm = 0;
  let sent = prompt("enter the text something")
  let sentence = String(sent);
  for(let i = 0; i < sentence.length; i++){
    if(i != 0){
      if(sentence[i] === " "){
        words++;
      }
    }
  }
  end = getSeconds();
  if(end < start){
    end += 3600;
  }
  timeDiff = end - start;
  wpm = words / timeDiff;
  return wpm * 60;
}
console.log("you type at " + wordCount() + " words per minute");
Enter fullscreen mode Exit fullscreen mode

Top comments (0)