Witam. Tworzę program w JavaScript który na podać ilość zdań, liter, słów oraz całą długość.
var letters;
var words;
var phrases;
var text;
var textLength;
var lastToken = "";
function process()
{
text = document.getElementById("inputText").value;
textLength = text.length;
for(i = 0; i <= text.length; i++)
{
if(text.charAt(i) == " " && lastToken != " ") words++;
else if(text.charAt(i) == "." || text.charAt(i) == "!" || text.charAt(i) == "?" && lastToken != "." && lastToken != "!" && lastToken != "?") phrases++;
else letters++;
lastToken = text.charAt(i);
}
document.getElementById("processResoult").innerHTML = "Litery: " + letters + " Wyrazy: " + words + " Zdania: " + phrases + " Długość: " + textLength;
}
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="utf-8">
<title>Słowa</title>
<link rel="stylesheet" href="style.css">
<link href="https://fonts.googleapis.com/css?family=Ubuntu:500&subset=latin-ext" rel="stylesheet">
</head>
<body>
<div id="inputs">
<input id="inputText" type="text" placeholder="Tekst">
<input id="processButton" type="button" value="Process" onclick="process()">
</div>
<div id="processResoult"></div>
<script src="scripts.js"></script>
</body>
</html>
Tylko sprawdzanie ilości liter daje poprawny wynik, inne dają wartość null lub undifined. Przeglądarka to Firefox, a system to Ubuntu.