I'm doing this for a school project but one thing is bugging me, there is a part of the project that requires me to change white space or just " " a space to a number. Here is my code:
I know its messy, I've only been coding for half a year
exclsp
is "exclude spaces"inclsp
is "include spaces"dispwos
is "display without spaces"dispwsp
is "display with spaces"var txt; var num; var spce = 0; function cnt() { txt = document.getElementById('disp').value; num = txt.length; // includes spaces into the returned number if (document.getElementById("inclsp").checked == true) { document.getElementById("dispwsp").innerHTML = num + " characters."; } // excludes spaces from the returned number if (document.getElementById("exclsp").checked === true) { for (var i = 0; i < num; i++) { if (txt.includes(" ")) { // alert("THERES A SPACE HERE"); spce++; } else { num = num; } } } document.getElementById("dispwos").innerHTML = num - spce + " characters."; }
<!doctype html> <html> <head> <meta charset="utf-8"> <script src="LetterCount.js"></script> <link rel="stylesheet" type="text/css" href="LetterCount.css"/> <title>Letter Counter</title> </head> <body> <textarea rows="4" cols="50" placeholder="Input your text here!" id="disp"></textarea><br> <form name="form1"> <input type="radio" name="button" id="inclsp"> Include spaces</input><br> <input type="radio" name="button" id="exclsp"> Exclude spaces</input><br> </form> <button onclick="cnt()">Click Me!</button><br><br> <div id="dispwsp"></div> <div id="dispwos"></div> </body> </html>