Mitchell Niesar
Mitchell Niesar

Reputation: 53

Without if statements how can I change "words" to word when there is only 1 word in the text area?

Currently, it defaults to always saying "Words:" & "Characters:". In the case of 1 word, "words" has to be changed to word. I am not allowed to use if statements only ternary operators. Any help would be appreciated. As is, the output shows "Word" no matter what and the same with character. It only needs to say "word"/"character" when there is 1 word and or 1 character and plural for the rest.

function countWords(self) {
  var myText = document.getElementById("myText");
  // Using regular expresisons, it matches the spaces in the text
  var spaces = self.value.match(/\S+/g);
  // If null set characters to 0, if not sets words to word length
  var words = spaces ? spaces.length : 0;
  var words = 1 ? document.getElementById("wordCount").innerHTML = "Word: " + words : document.getElementById("wordCount").innerHTML = "Words: " + words;

  // Splits text into characters
  var chars = myText.value.split('');
  // Counts the amount of charcters and saves
  var charCount = chars.length;
  // Updates the character Count
  document.getElementById("charCount").innerHTML = "Characters: " + charCount;
  // Updates the word count
  // document.getElementById("wordCount").innerHTML = "Words: " + words;
  var chars = 1 ? document.getElementById("charCount").innerHTML = "Character: " + words : document.getElementById("charCount").innerHTML = "Characters: " + words;
}
<h1>Type Your Words In The Text Area Below</h1>

<textarea id="myText" rows="10" cols="50" style="text-transform: uppercase" onkeyup="countWords(this);"></textarea>
<br>
<span id="wordCount">Word: </span>
<br>
<span id="charCount">Character: </span>

Upvotes: 0

Views: 43

Answers (1)

CertainPerformance
CertainPerformance

Reputation: 370819

Inside a template literal, you can check if words > 1, and depending on whether that's true or false, concatenate an 's' or the empty string:

document.getElementById("wordCount").innerHTML = `Word${words > 1 ? 's' : ''}: ${words}`;

function countWords(self) {
  var myText = document.getElementById("myText");
  // Using regular expresisons, it matches the spaces in the text
  var spaces = self.value.match(/\S+/g);
  // If null set characters to 0, if not sets words to word length
  var words = spaces ? spaces.length : 0;


  // Splits text into characters
  var chars = myText.value.split('');
  // Counts the amount of charcters and saves
  var charCount = chars.length;
  // Updates the character Count
  document.getElementById("charCount").innerHTML = "Characters: " + charCount;
  // Updates the word count
  document.getElementById("wordCount").innerHTML = `Word${words > 1 ? 's' : ''}: ${words}`;
}
<h1>Type Your Words In The Text Area Below</h1>

<textarea id="myText" rows="10" cols="50" style="text-transform: uppercase" onkeyup="countWords(this);"></textarea>
<br>
<span id="wordCount"></span>
<br>
<span id="charCount"></span>

Upvotes: 1

Related Questions