Construct sentence with JS

Hello, not sure if this is an easy question:
I have input where the user can write anything. There’s a note for the user that says if he uses a comma, that will separate in different sentences.

So I need to get the value and divide the content depending on commas.

How I need to construct the sentence: [ 'Something', 'Something2', 'Something3', ... ]

Example without the comma:
Hello my name is Pikachu.
Result: [ 'Hello my name is Pikachu.' ]

Example with comma:
Hello, my name is Pikachu.
Result: [ 'Hello', 'my name is Pikachu.' ]

Its possible to create a detector when no commas, return the simple sentence quoted, and if there are commas divide the text into sentences and quoting the text?

Thanks!

Yes, you can split the string by the quote.

var array = str.split(",");

Thanks, @Paul_Wilkins

But I don’t see any difference:

Maybe i’m doing something wrong?

No, you’re not doing anything wrong. Array items are shown with a comma separating each item.

But with the quotes.
Need to add quotes to each sentence.
Like: ‘sentence one’, ‘sentence two’

Thanks.

You can wrap the items in the array with quotes.

array = array.map(function (str) {
    return "\"" + str + "\"";
});

Some prefer to use '"' for quotes, others prefer to use "'" for quotes.
I prefer to use "\"" which maintains the coding guidelines of using double quotes, and the escaped quote helps to make it explicit what is being added there.

Using your example, that becomes:

function myFunction() {
    var str = "Hello, how are you doing, today?";
    var res = str.split(",");
    res = res.map(function (phrase) {
        return "\"" + phrase + "\"";
    });
    document.getElementById("demo").innerHTML = res;
}

If you want the spaces after the comma removed too, you could use a regular expression to also optionally include spaces as well…

    // var res = str.split(",");
    var res = str.split(/, ?/);
function myFunction() {
    var str = "Hello, how are you doing, today?";
    var res = str.split(/, ?/);
    res = res.map(function (phrase) {
        return "\"" + phrase + "\"";
    });
    document.getElementById("demo").innerHTML = res;
}

And if you want the displayed text to have a space after each comma, you can join the array together using ", " as the joining term.

    // document.getElementById("demo").innerHTML = res;
    document.getElementById("demo").innerHTML = res.join(", ");

which ends up being:

function myFunction() {
    var str = "Hello, how are you doing, today?";
    var res = str.split(/, ?/);
    res = res.map(function (phrase) {
        return "\"" + phrase + "\"";
    });
    document.getElementById("demo").innerHTML = res.join(", ");
}

Thanks so much!!

Now i’m trying to add the result to the plugin that i want to use but i’m getting error:

When focus the input, should start to play this animation but is returning error when focused.

So i think is not getting correctly the data.

The res variable is already array, so nesting that inside of another array on the sentences line is not expected by the superplaceholder code.

But I’m not nesting anything.

I should remove the res.map?

var str = "Hello, how are you doing, today?";
var res = str.split(/, ?/);
res = res.map(function (phrase) {
        return "\"" + phrase + "\"";
    });

sentences: [ res ]

Thanks!

Yes you are.

The res variable is an array.

var res = str.split(/, ?/); // ["first", "second", "third"]

Then when you give it as information to the superplaceholder, you are putting it inside of an array.

sentences: [ res ] // [ ["first", "second". "third"] ]

There you have one array nested inside of another array.
When superplaceholder gets the first item in the array, it expects “first” but instead gets [“first”, “second”, “third”] and doesn’t know how to deal with that.

If you want it to work, remove the array brackets from the sentences line.

sentences: res // ["first", "second". "third"]

That way when the superplaceholder gets the first item, it gets “first” and knows what to do with that.

1 Like

Wow, something that simple was causing that.
How did you know there was doing 2 times the [ ?
Do you use any tool for debugging?
Can you recommend me any tool if yes?

Well, the superplaceholder expects an array of strings, and you were putting an array of strings inside of an array, so you had an array of an array of strings. It was self-evident really, and a simple test revealed a working solution.

Then it was just a matter of explaining so-as to help you understand what was causing the problem too.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.