Page 380 - Web_Application_v2.0_C12_Fb
P. 380
</HEAD>
<BODY>
<H1>Print Even Length Words</H1>
<FORM>
<INPUT TYPE="TEXT" ID="INPUTSTRING" PLACEHOLDER="Enter a string">
<BUTTON TYPE="BUTTON" ONCLICK="printevenlengthwords()">Submit</BUTTON>
</FORM>
<DIV ID="OUTPUT"></DIV>
<SCRIPT>
function printevenlengthwords() {
var inputstring = document.getElementById("INPUTSTRING").value;
var output = "";
var wordlength = 0;
for (var i = 0; i < inputstring.length; i++) {
if (inputstring.charAt(i) == " ") {
if (wordlength % 2 == 0) {
output = output.concat(inputstring.substring(i-wordlength, i) + " ");
}
wordlength = 0;
} else {
wordlength++;
}
}
if (wordlength % 2 == 0) {
output = output.concat(inputstring.substring(inputstring.length-wordlength));
}
document.getElementById("OUTPUT").innerHTML = output;
}
</SCRIPT>
</BODY>
</HTML>
Output of the preceding code is as follows:
378 Touchpad Web Applications (Ver. 2.0)-XII

