0

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>
Shri Suresh
  • 463
  • 1
  • 5
  • 20
RHJames
  • 3
  • 1

6 Answers6

0

I think you need to change this line:

 if (txt.includes(" "))

to

 if (txt[i] == " ")

so that you're actually checking each character rather that attempting to examine the whole string each time. You could also use a regular expression and do it in one simple line of code and eliminate the loop altogether:

spce = txt.match(/\s/g).length
Alastair Brown
  • 1,598
  • 8
  • 12
0

I don't understand the purpose of the dispwsp dispwos so I just removed them. You only have 1 result you want to display so why put it in different places just make one div for your result, like

 <div id="result"></div>

And your JS can be simplified a lot, you don't need to loop through the letters. Here's the fiddle: https://jsfiddle.net/zwzqmd27/

function cnt() {
  var inputText = document.getElementById("disp").value;
  if (document.getElementById("exclsp").checked) //exclude spaces
  {
    document.getElementById("result").innerHTML = inputText.split(" ").join("").length + " characters";
  } 
  else //include spaces
  {
    document.getElementById("result").innerHTML = inputText.length + " characters";
  }
}
Keatinge
  • 4,330
  • 6
  • 25
  • 44
0

Possible duplicate of Check if a string has white space

But you can try this.

function hasWhiteSpace(s) {
  return s.indexOf(' ') >= 0;
}
Community
  • 1
  • 1
Michael Melin
  • 13
  • 1
  • 4
0

If You want to change a white space in a string to a number..

This could possibly help you ...

str.replace(/\s/g,"9");//any number(that You want)

This piece of code is basically replaces the white space with a number..

0

As @Micheal said, you can use indexOf() method to check if particular character(s) is present in your text content.

You just need to pass the character or substring(set of characters) to check if it is present.

Example : 
var myText = "Sample text";
var substringIndex = myText.indexof(" "); //substringIndex = 6
substringIndex = mytext.indexof("ex");//substringIndex  = 8;
substringIndex  = mytext.indexof("tt"); // substringIndex  =-1;

If substring doesn't matches, it will return -1 as index. By using index you can say, if particular character(substring) presents if index value is greater than -1.

Note : If u pass set of characters, it will return only the starting index of the first character if entire set matches.

In your case, it would be like

 ...........
   ...........
     if (txt.indexOf(" ")>-1)
        {
            // alert("THERES A SPACE HERE");
            spce++;
        }
        else
        {
            num = num;
        }
      ...............
   ...............
Unknown
  • 531
  • 5
  • 18
0

Just replace script with code bellow.. I do it for you...

var txt;
var num;
var spce = 0;

function cnt()
{
//to clear "dispwsp" and "dispwos" before action in cnt() function
document.getElementById("dispwsp").innerHTML = "";
document.getElementById("dispwos").innerHTML = "";

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)
{
    num = 0;
    spce = 0;
    for (var i = 0; i < txt.length; i++) {
        var temp = txt.substring(i, (i+1)); 
            if(temp==" ")
            {
              spce++;
            }else
            {
              num++;
            }
            document.getElementById("dispwos").innerHTML = num  + " characters and "+ spce +" spces ";
        } 
    }   
}