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?
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.
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;
}
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(", ");
}
var str = "Hello, how are you doing, today?";
var res = str.split(/, ?/);
res = res.map(function (phrase) {
return "\"" + phrase + "\"";
});
sentences: [ res ]
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.
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.