Home > Software design >  How to count sentences next to each other that starts with the word "The" in Javascript
How to count sentences next to each other that starts with the word "The" in Javascript

Time:07-31

I a beginner and I am trying to write a code that first splits a pasted text into sentences and then checks if three (or more) sentences next to each other starts with the word "The". I also would like the program to work regardless of how many sentences that pasted text consist of, now I have only 5 sentences. Is there anyone that can help me?

<!DOCTYPE html>
<html>
<body>

See if three (or more) sentences next to each other starts with "The".





<form id = "quiz" name = "quiz">

<input id = "text1" type = "text" name = "question1"> <!here you are supposed to paste the text you want to check>
<input id = "button" type = "button" value = "Split into sentences" onclick = "Split();">




<p id="demo1"></p>
<p id="demo2"></p>
<p id="demo3"></p>





<script>
function Split() {
 question1 = document.quiz.question1.value; 
let text = question1;
const myArray = text.split(/[\\.!?]/); //this splits the text into sentences
var x=0
var y=x

document.getElementById("demo1").innerHTML = myArray; //here I write the sentences

let result1 = myArray [0].startsWith("The"); //here I don't have a space before the word "The" as a text normally don't start with a space.
let result2 = myArray [1].startsWith(" The");
let result3 = myArray [2].startsWith(" The");
let result4 = myArray [3].startsWith(" The");
let result5 = myArray [4].startsWith(" The"); //now I only have five sentences but I would like the program to check the text regardless of how many sentences the pasted text includes. How do I achieve that?




{document.getElementById("demo3").innerHTML = 'You have ' (result1   result2   result3   result4   result5)   ' sentences that starts with "The".';} // Here I count the sentences that starts with "The". But I would like to only count if three (or more) sentences next to each other starts with "The" and to let the program write something like "You have three sentences (or more) next to each other that starts with 'The'" and to inform the user of the program which the first sentence of these three (or more) consecutive sentences that starts with "The" is.





}
</script>

</body>
</html>

CodePudding user response:

Try this at the end of your Split function!

let results=[];
for (var i=0;i<myArray.length;i  )
{
   if (myArray[i].toLowerCase().startsWith("the") || myArray[i].toLowerCase().startsWith(" the")) results.push(myArray[i]);
}
document.getElementById("demo3").innerHTML = 'You have ' (results.length)   ' sentences that starts with "The".';

Basically we loop through the results and check each one, adding it to a new array if it matches. The length of the array is how many matches there are. You could also avoid the startsWith(" the") if you tweak your regex to also ignore leading spaces.

CodePudding user response:

If you notice you are using the same sequence of code across multiple lines. Since your new to coding a good rule of thumb is: If you see yourself typing the same thing over and over or copy and pasting; a loop is needed! So try this.

Since we want to count how many sentence there are we can safely assume that every sentence ends in a period, so we can iterate through and count periods. That's as simple as using regex which is pain of its own but can be very useful like now.

let sentenceLength = (text.match(/[.]/).length ; //regex

By the way regex can be learned here with very good explanations: Regexr Next now that we found how many sentences there we can simply throw your lines in a for loop.

let results = 0;
for(let i=0; i < sentenceLength; i  ){
   if(myArray [i].startsWith("The")){
      result   ;
}

Now this code will guarantee doesn't matter how many lines it will iterate through. Again I suggest you really look into the concept looping saves a lot of typing and time: looping article. Also you may have noticed that I didn't answer your last question because the thing about coding is problem solving analyze what we did above and determine how might that question fit here keep grinding and I believe in you! Happy coding. PS: Research is a very strong skill and implementing what your read. Developers typically get paid to do research then code.

  • Related