3

Please have a look at the below code

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">

        <script>

            function ttsSpeak(text)
            {

                var textArray = text.split(" ");
                var finalString="";

                for(var i=0;i<textArray.length;i++)
                {
                    finalString = finalString+textArray[i]+"+";
                }

                alert(finalString);
            }
        </script>

    </head>
    <body>
        <div><p id="text">In meteorology surface.
            </p></div>
        <div><button id="speak" onclick="ttsSpeak(document.getElementById('text').innerHTML);">Speak</button></div>
    </body>
</html>

In here, my expected output is,

In+meteorology+surface+

But instead, I got the below

enter image description here

What have I done wrong here?

0

6 Answers 6

2

To help you visualise the reason, if you replace all the spaces in your HTML with an underscore, your text paragraph looks like this:

<p id="text">In_meteorology_surface.
____________</p>

with a newline after your text, and a bunch of spaces after it on the second line.

You either want to trim the text before you split it, or you want to split on possibly multiple spaces.

Also, you probably don't want to use innerHTML, but textContent.

Sign up to request clarification or add additional context in comments.

Comments

2

The innerHTML contains all the whitespace (i.e. " ") at the end - this is being split to empty strings ("") which are then joined at the end.

The general approach to splitting on white-space as such is to use /\s+/ (split on as much white-space as possible), possibly preceded by a trim.

"foo     ".split(" ")          // -> ["foo", "", "", "", "", ""]
"foo     ".split(/\s+/)        // -> ["foo", ""]
"foo     ".trim().split(/\s+/) // -> ["foo"]

Using /\s+/ will also "fix" the newline issue implicitly.

(Also, see textContent as pointed out by Amadan.)

Comments

1

With the statement

finalString = finalString+textArray[i]+"+";

you're adding the plus sign after every item in your textArray, even in the last iteration. You might use join() instead.


The markup

<p id="text">In meteorology surface.
            </p>

does contain lots of whitespaces you don't want. Remove them by calling trim() on the string before splitting it.

alert(text.trim().split(" ").join("+")+"+");

Comments

1

As prompted above, there is additional white space. In future, to avoid unnoticeable whitespace when using split, you can use the trim() function before split.

In your case:

text = text.trim();
var textArray = text.split(" ");

Comments

0

Your textArray has elements with their value = " " , a space, because of(between . and </p>):

<p id="text">In meteorology surface.
            </p>

(if there n number of spaces, then your textArray will have n/2 number of elements which their values are just a space; if the n is even and (n/2 - 1) number of elements which their values are just a space if the n is odd)

In php you can use array_filter() to remove these empty objects from the array.

In javascript you can do as follows:

function isSpace(element){
 if(element != " " || element != "") return element;
}
textArrayWithoutEmptyObjects = textArray.filter(isSpace));

Further Reading about array filter in javascript is here

Comments

0

In for loop,you can write this finalString = finalString+textArray[i]+"+"; and it will give your expected output.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.