[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

some logic error on a map function

jrough

6/16/2016 3:24:00 AM

I get the error : unexpected end of input, there is some logic error before it joins the words.

'use strict';
var sentences = [
"I now took the measure of the bench, and found that it was a foot too short; but that could be mended with a chair.",
"But it was a foot too narrow, and the other bench in the room was about four inches higher than the planed one--so there was no yoking them.",
"I then placed the first bench lengthwise along the only clear space against the wall, leaving a little interval between, for my back to settle down in.",
"But I soon found that there came such a draught of cold air over me from under the sill of the window, that this plan would never do at all, especially as another current from the rickety door met the one from the window, and both together formed a series of small whirlwinds in the immediate vicinity of the spot where I had thought to spend the night.",
"The devil fetch that harpooneer, thought I, but stop, couldn't I steal a march on him--bolt his door inside, and jump into his bed, not to be wakened by the most violent knockings? It seemed no bad idea; but upon second thoughts I dismissed it.",
"For who could tell but what the next draught, so soon as I popped out of the room, the harpooneer might be standing in the entry, all ready to knock me down!"
];

var badwords = ['window', 'chair', 'knockings'];


// Fill in function body here
var hasBadwords = function (message, index ) {
var words = message.split(" ");
words.map(function(word,index){
if (word == 'window' || word == 'chair' || word == 'knockings'){
return true;
} else {
return false;}
});

// Tell us what the output is from running this code:
console.log(sentences.map(function (sentence, index) {
return hasBadwords(sentence) ? index : '';
}).join(''));
10 Answers

Jake Jarvis

6/16/2016 7:59:00 AM

0

Am 16.06.2016 um 05:23 schrieb JRough:
> I get the error : unexpected end of input, there is some logic error before it joins the words.
>
> 'use strict';
> var sentences = [
> "I now took the measure of the bench, and found that it was a foot too short; but that could be mended with a chair.",
> "But it was a foot too narrow, and the other bench in the room was about four inches higher than the planed one--so there was no yoking them.",
> "I then placed the first bench lengthwise along the only clear space against the wall, leaving a little interval between, for my back to settle down in.",
> "But I soon found that there came such a draught of cold air over me from under the sill of the window, that this plan would never do at all, especially as another current from the rickety door met the one from the window, and both together formed a series of small whirlwinds in the immediate vicinity of the spot where I had thought to spend the night.",
> "The devil fetch that harpooneer, thought I, but stop, couldn't I steal a march on him--bolt his door inside, and jump into his bed, not to be wakened by the most violent knockings? It seemed no bad idea; but upon second thoughts I dismissed it.",
> "For who could tell but what the next draught, so soon as I popped out of the room, the harpooneer might be standing in the entry, all ready to knock me down!"
> ];
>
> var badwords = ['window', 'chair', 'knockings'];
>
>
> // Fill in function body here
> var hasBadwords = function (message, index ) {
> var words = message.split(" ");
> words.map(function(word,index){
> if (word == 'window' || word == 'chair' || word == 'knockings'){
> return true;
> } else {
> return false;}
> });
>
> // Tell us what the output is from running this code:
> console.log(sentences.map(function (sentence, index) {
> return hasBadwords(sentence) ? index : '';
> }).join(''));
>

The error message apparently is "Uncaught SyntaxError: Unexpected end of
input" and it is used by at least Chrome.

In Firefox the message is different: "SyntaxError: missing } after
function body".

Do you use an editor with syntax highlighting?

Evertjan.

6/16/2016 10:47:00 AM

0

Jake Jarvis <pig_in_shoes@yahoo.com> wrote on 16 Jun 2016 in
comp.lang.javascript:

>
> The error message apparently is "Uncaught SyntaxError: Unexpected end of
> input" and it is used by at least Chrome.
>
> In Firefox the message is different: "SyntaxError: missing } after
> function body".
>
> Do you use an editor with syntax highlighting?
>

var hasBadwords = function (message, index ) {
var words = message.split(" ");
words.map(function(word,index){
if (word == 'window' || word == 'chair' || word == 'knockings'){
return true;
} else {
return false;}
});
}; <<<<<<<<<<< MISSING } indeed!


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Thomas 'PointedEars' Lahn

6/16/2016 10:56:00 AM

0

Jake Jarvis wrote:

[Pretty-printed code]

> > // Fill in function body here
> > var hasBadwords = function (message, index) {
> > var words = message.split(" ");
> > words.map(function (word,index) {
> > if (word == 'window' || word == 'chair' || word == 'knockings') {
> > return true;
> > } else {
> > return false;}
> > }
> > );
^-- }
> >
^-- };

> > // Tell us what the output is from runni ng this code:
> > console.log(sentences.map(function (sentence, index) {
> > return hasBadwords(sentence) ? index : '';
> > }).join(''));
> >

> The error message apparently is "Uncaught SyntaxError: Unexpected end of
> input" and it is used by at least Chrome.
>
> In Firefox the message is different: "SyntaxError: missing } after
> function body".
>
> Do you use an editor with syntax highlighting?

Wasted effort. The record shows that â??JRoughâ? is a script-kiddie: they do
not program, they copy & pray.

For example, look closely at that code. (Observe the comments, too.) Not
only is the program logic overly complicated (you could just return the
result of the boolean expression), it is completely bogus.

Even if you insert the missing â??}â?s, words.map() and therefore hasBadWords()
do *nothing of consequence*. The â??returnâ? statement returns *from the
anonymous callback*, not from hasBadWords(), the callback makes no changes,
and the return value of words.map() is not even used. As a result,
hasBadWords() would always return â??undefinedâ? which is always converted to
â??falseâ?, and so console.log() would always print an Array of empty strings.

Of course, this is *the* use-case for regular expressions instead¹:

var
badWords = ["window", "chair", "knockings"],
rxBadWords = new RegExp(
"\\b(?:"
+ badWords.sort(function (a, b) {
/*
* Sort words by length, longest ones first,
* so that â??windowsâ? is preferred over â??windowâ?
*/
return b.length - a.length;
}).join("|")
+ ")\\b",
"g");

console.log(sentences.map(function (sentence) {
return sentence.match(rxBadWords);
}));

[console.log() will then print â??nullâ? if there is no match in a sentence, a
representation of an Array of matches (with duplicates) otherwise. This can
be refined to give, e.g. the index of the matches in a sentence, by using
RegExp.prototype.exec() in a loop instead of String.prototype.match().]

â??JRoughâ? has been told this before, but they would not listen.

______
¹ As for /\b/, it is possible to support non-ASCII characters with the
/(?:^|[^â?¦])/ and /(?:[^â?¦]|$)/ subexpressions instead. JSX:regexp.js [1]
conveniently supports /\w/, /\b/, and /\p{â?¦}/ in general, for
non-ASCII characters with the "u" flag for Unicode mode, using this
approach.

[It had set the â??flagsâ? property on the returned RegExp instance,
but as of ECMAScript 2015, RegExp instances have a *built-in*
*read-only* â??flagsâ? property. [2] So JSX:regexp.js is now setting
the â??_flagsâ? property instead in case you are interested in the
non-standard flags that it supports. (â??_flagsâ? will probably be
read-only, too, in a later revision.)

As a point of note, the â??yâ? (sticky) flag is now part of the standard.
So is the â??uâ? (Unicode) flag [3], but Google V8 JavaScript 4.9.385 in
Chromium 49.0.2623.87 [4] does not implement it (SyntaxError).
V8 5.0.71 in Chromium 50.0.2661.94 and V8 5.1.281 in
Chromium 51.0.2704.79 (stable) do, but not in a way that /\w/ matches
non-ASCII letters or that /\b/ does not match adjacent non-ASCII
letters. This appears to be standards-compliant â?? one wonders why the
â??uâ? flag was standardized in the first place.

For now I recommend to keep using libraries like JSX:regexp.js for
*proper* Unicode support.]

[1] <http://PointedEars.de/scripts/test/...
[2] <http://www.ecma-international.org/ecma-262/6.0/#sec-get-regexp.prototype...
[3] <http://www.ecma-international.org/ecma-262/6.0/#sec-regexpbuilt...
[4] <https://en.wikipedia.org/wiki/Google_Chrome_release_h...
--
PointedEars
FAQ: <http://PointedEars.... | SVN: <http://PointedEars.de...
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
Please do not cc me. / Bitte keine Kopien per E-Mail.

Thomas 'PointedEars' Lahn

6/16/2016 12:08:00 PM

0

Thomas 'PointedEars' Lahn wrote:

> Jake Jarvis wrote:
> [Pretty-printed code]

Incompletely, therefore:

>> > // Fill in function body here
>> > var hasBadwords = function (message, index) {
>> > var words = message.split(" ");
>> > words.map(function (word,index) {
>> > if (word == 'window' || word == 'chair' || word == 'knockings') {
>> > return true;
>> > } else {
>> > return false;}
^
*There* is the first "missing" â??}â? â?¦

>> > }
>> > );
> ^-- }

â?¦ so this insertion is not necessary.

>> >
> ^-- };

However, this is.

Still, it does not change the fact that the program logic is bogus, as I
explained before.

--
PointedEars
FAQ: <http://PointedEars.... | SVN: <http://PointedEars.de...
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
Please do not cc me. / Bitte keine Kopien per E-Mail.

jrough

6/16/2016 6:04:00 PM

0

On Thursday, June 16, 2016 at 3:56:03 AM UTC-7, Thomas 'PointedEars' Lahn wrote:
> Jake Jarvis wrote:
>
> [Pretty-printed code]
>
> > > // Fill in function body here
> > > var hasBadwords = function (message, index) {
> > > var words = message.split(" ");
> > > words.map(function (word,index) {
> > > if (word == 'window' || word == 'chair' || word == 'knockings') {
> > > return true;
> > > } else {
> > > return false;}
> > > }
> > > );
> ^-- }
> > >
> ^-- };
>
> > > // Tell us what the output is from runni ng this code:
> > > console.log(sentences.map(function (sentence, index) {
> > > return hasBadwords(sentence) ? index : '';
> > > }).join(''));
> > >
>
> > The error message apparently is "Uncaught SyntaxError: Unexpected end of
> > input" and it is used by at least Chrome.
> >
> > In Firefox the message is different: "SyntaxError: missing } after
> > function body".
> >
> > Do you use an editor with syntax highlighting?
>
> Wasted effort. The record shows that “JRough” is a script-kiddie: they do
> not program, they copy & pray.
>
> For example, look closely at that code. (Observe the comments, too.) Not
> only is the program logic overly complicated (you could just return the
> result of the boolean expression), it is completely bogus.
>
> Even if you insert the missing “}”s, words.map() and therefore hasBadWords()
> do *nothing of consequence*. The “return” statement returns *from the
> anonymous callback*, not from hasBadWords(), the callback makes no changes,
> and the return value of words.map() is not even used. As a result,
> hasBadWords() would always return “undefined” which is always converted to
> “false”, and so console.log() would always print an Array of empty strings.
>
> Of course, this is *the* use-case for regular expressions instead¹:
>
> var
> badWords = ["window", "chair", "knockings"],
> rxBadWords = new RegExp(
> "\\b(?:"
> + badWords.sort(function (a, b) {
> /*
> * Sort words by length, longest ones first,
> * so that “windows” is preferred over “window”
> */
> return b.length - a.length;
> }).join("|")
> + ")\\b",
> "g");
>
> console.log(sentences.map(function (sentence) {
> return sentence.match(rxBadWords);
> }));
>
> [console.log() will then print “null” if there is no match in a sentence, a
> representation of an Array of matches (with duplicates) otherwise. This can
> be refined to give, e.g. the index of the matches in a sentence, by using
> RegExp.prototype.exec() in a loop instead of String.prototype.match().]
>
> “JRough” has been told this before, but they would not listen.
>
> ______
> ¹ As for /\b/, it is possible to support non-ASCII characters with the
> /(?:^|[^…])/ and /(?:[^…]|$)/ subexpressions instead. JSX:regexp.js [1]
> conveniently supports /\w/, /\b/, and /\p{…}/ in general, for
> non-ASCII characters with the "u" flag for Unicode mode, using this
> approach.
>
> [It had set the “flags” property on the returned RegExp instance,
> but as of ECMAScript 2015, RegExp instances have a *built-in*
> *read-only* “flags” property. [2] So JSX:regexp.js is now setting
> the “_flags” property instead in case you are interested in the
> non-standard flags that it supports. (“_flags” will probably be
> read-only, too, in a later revision.)
>
> As a point of note, the “y” (sticky) flag is now part of the standard.
> So is the “u” (Unicode) flag [3], but Google V8 JavaScript 4.9.385 in
> Chromium 49.0.2623.87 [4] does not implement it (SyntaxError).
> V8 5.0.71 in Chromium 50.0.2661.94 and V8 5.1.281 in
> Chromium 51.0.2704.79 (stable) do, but not in a way that /\w/ matches
> non-ASCII letters or that /\b/ does not match adjacent non-ASCII
> letters. This appears to be standards-compliant – one wonders why the
> “u” flag was standardized in the first place.
>
> For now I recommend to keep using libraries like JSX:regexp.js for
> *proper* Unicode support.]
>
> [1] <http://PointedEars.de/scripts/test/...
> [2] <http://www.ecma-international.org/ecma-262/6.0/#sec-get-regexp.prototype...
> [3] <http://www.ecma-international.org/ecma-262/6.0/#sec-regexpbuilt...
> [4] <https://en.wikipedia.org/wiki/Google_Chrome_release_h...
> --
> PointedEars
> FAQ: <http://PointedEars.... | SVN: <http://PointedEars.de...
> Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
> Please do not cc me. / Bitte keine Kopien per E-Mail.

Okay, thanks, the logic is better to have a regular expression instead of a second map.

jrough

6/16/2016 6:05:00 PM

0

On Thursday, June 16, 2016 at 3:56:03 AM UTC-7, Thomas 'PointedEars' Lahn wrote:
> Jake Jarvis wrote:
>
> [Pretty-printed code]
>
> > > // Fill in function body here
> > > var hasBadwords = function (message, index) {
> > > var words = message.split(" ");
> > > words.map(function (word,index) {
> > > if (word == 'window' || word == 'chair' || word == 'knockings') {
> > > return true;
> > > } else {
> > > return false;}
> > > }
> > > );
> ^-- }
> > >
> ^-- };
>
> > > // Tell us what the output is from runni ng this code:
> > > console.log(sentences.map(function (sentence, index) {
> > > return hasBadwords(sentence) ? index : '';
> > > }).join(''));
> > >
>
> > The error message apparently is "Uncaught SyntaxError: Unexpected end of
> > input" and it is used by at least Chrome.
> >
> > In Firefox the message is different: "SyntaxError: missing } after
> > function body".
> >
> > Do you use an editor with syntax highlighting?
>
> Wasted effort. The record shows that “JRough” is a script-kiddie: they do
> not program, they copy & pray.
>
> For example, look closely at that code. (Observe the comments, too.) Not
> only is the program logic overly complicated (you could just return the
> result of the boolean expression), it is completely bogus.
>
> Even if you insert the missing “}”s, words.map() and therefore hasBadWords()
> do *nothing of consequence*. The “return” statement returns *from the
> anonymous callback*, not from hasBadWords(), the callback makes no changes,
> and the return value of words.map() is not even used. As a result,
> hasBadWords() would always return “undefined” which is always converted to
> “false”, and so console.log() would always print an Array of empty strings.
>
> Of course, this is *the* use-case for regular expressions instead¹:
>
> var
> badWords = ["window", "chair", "knockings"],
> rxBadWords = new RegExp(
> "\\b(?:"
> + badWords.sort(function (a, b) {
> /*
> * Sort words by length, longest ones first,
> * so that “windows” is preferred over “window”
> */
> return b.length - a.length;
> }).join("|")
> + ")\\b",
> "g");
>
> console.log(sentences.map(function (sentence) {
> return sentence.match(rxBadWords);
> }));
>
> [console.log() will then print “null” if there is no match in a sentence, a
> representation of an Array of matches (with duplicates) otherwise. This can
> be refined to give, e.g. the index of the matches in a sentence, by using
> RegExp.prototype.exec() in a loop instead of String.prototype.match().]
>
> “JRough” has been told this before, but they would not listen.
>
> ______
> ¹ As for /\b/, it is possible to support non-ASCII characters with the
> /(?:^|[^…])/ and /(?:[^…]|$)/ subexpressions instead. JSX:regexp.js [1]
> conveniently supports /\w/, /\b/, and /\p{…}/ in general, for
> non-ASCII characters with the "u" flag for Unicode mode, using this
> approach.
>
> [It had set the “flags” property on the returned RegExp instance,
> but as of ECMAScript 2015, RegExp instances have a *built-in*
> *read-only* “flags” property. [2] So JSX:regexp.js is now setting
> the “_flags” property instead in case you are interested in the
> non-standard flags that it supports. (“_flags” will probably be
> read-only, too, in a later revision.)
>
> As a point of note, the “y” (sticky) flag is now part of the standard.
> So is the “u” (Unicode) flag [3], but Google V8 JavaScript 4.9.385 in
> Chromium 49.0.2623.87 [4] does not implement it (SyntaxError).
> V8 5.0.71 in Chromium 50.0.2661.94 and V8 5.1.281 in
> Chromium 51.0.2704.79 (stable) do, but not in a way that /\w/ matches
> non-ASCII letters or that /\b/ does not match adjacent non-ASCII
> letters. This appears to be standards-compliant – one wonders why the
> “u” flag was standardized in the first place.
>
> For now I recommend to keep using libraries like JSX:regexp.js for
> *proper* Unicode support.]
>
> [1] <http://PointedEars.de/scripts/test/...
> [2] <http://www.ecma-international.org/ecma-262/6.0/#sec-get-regexp.prototype...
> [3] <http://www.ecma-international.org/ecma-262/6.0/#sec-regexpbuilt...
> [4] <https://en.wikipedia.org/wiki/Google_Chrome_release_h...
> --
> PointedEars
> FAQ: <http://PointedEars.... | SVN: <http://PointedEars.de...
> Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
> Please do not cc me. / Bitte keine Kopien per E-Mail.

thanks,

jrough

6/17/2016 8:30:00 PM

0

On Thursday, June 16, 2016 at 3:56:03 AM UTC-7, Thomas 'PointedEars' Lahn wrote:
> Jake Jarvis wrote:
>
> [Pretty-printed code]
>
> > > // Fill in function body here
> > > var hasBadwords = function (message, index) {
> > > var words = message.split(" ");
> > > words.map(function (word,index) {
> > > if (word == 'window' || word == 'chair' || word == 'knockings') {
> > > return true;
> > > } else {
> > > return false;}
> > > }
> > > );
> ^-- }
> > >
> ^-- };
>
> > > // Tell us what the output is from runni ng this code:
> > > console.log(sentences.map(function (sentence, index) {
> > > return hasBadwords(sentence) ? index : '';
> > > }).join(''));
> > >
>
> > The error message apparently is "Uncaught SyntaxError: Unexpected end of
> > input" and it is used by at least Chrome.
> >
> > In Firefox the message is different: "SyntaxError: missing } after
> > function body".
> >
> > Do you use an editor with syntax highlighting?
>
> Wasted effort. The record shows that “JRough” is a script-kiddie: they do
> not program, they copy & pray.
>
> For example, look closely at that code. (Observe the comments, too.) Not
> only is the program logic overly complicated (you could just return the
> result of the boolean expression), it is completely bogus.
>
> Even if you insert the missing “}”s, words.map() and therefore hasBadWords()
> do *nothing of consequence*. The “return” statement returns *from the
> anonymous callback*, not from hasBadWords(), the callback makes no changes,
> and the return value of words.map() is not even used. As a result,
> hasBadWords() would always return “undefined” which is always converted to
> “false”, and so console.log() would always print an Array of empty strings.
>
> Of course, this is *the* use-case for regular expressions instead¹:
>
> var
> badWords = ["window", "chair", "knockings"],
> rxBadWords = new RegExp(
> "\\b(?:"
> + badWords.sort(function (a, b) {
> /*
> * Sort words by length, longest ones first,
> * so that “windows” is preferred over “window”
> */
> return b.length - a.length;
> }).join("|")
> + ")\\b",
> "g");
>
> console.log(sentences.map(function (sentence) {
> return sentence.match(rxBadWords);
> }));
>
> [console.log() will then print “null” if there is no match in a sentence, a
> representation of an Array of matches (with duplicates) otherwise. This can
> be refined to give, e.g. the index of the matches in a sentence, by using
> RegExp.prototype.exec() in a loop instead of String.prototype.match().]
>
okay, I tried this bit of your program adding the loop with the RegExp.prototype.exec() loop. I want it to print out the words.
console.log(sentences.map(function (sentence) {
// return sentence.match(rxBadWords);
var words = sentence.split(" ");
for (var i; i< words.length; i++){
rxBadWords.exec(words);
}

}));

doesn't seem to work, is this what you meant? tnx,

jrough

6/17/2016 8:32:00 PM

0

On Thursday, June 16, 2016 at 12:59:00 AM UTC-7, Jake Jarvis wrote:
> Am 16.06.2016 um 05:23 schrieb JRough:
> > I get the error : unexpected end of input, there is some logic error before it joins the words.
> >
> > 'use strict';
> > var sentences = [
> > "I now took the measure of the bench, and found that it was a foot too short; but that could be mended with a chair.",
> > "But it was a foot too narrow, and the other bench in the room was about four inches higher than the planed one--so there was no yoking them.",
> > "I then placed the first bench lengthwise along the only clear space against the wall, leaving a little interval between, for my back to settle down in.",
> > "But I soon found that there came such a draught of cold air over me from under the sill of the window, that this plan would never do at all, especially as another current from the rickety door met the one from the window, and both together formed a series of small whirlwinds in the immediate vicinity of the spot where I had thought to spend the night.",
> > "The devil fetch that harpooneer, thought I, but stop, couldn't I steal a march on him--bolt his door inside, and jump into his bed, not to be wakened by the most violent knockings? It seemed no bad idea; but upon second thoughts I dismissed it.",
> > "For who could tell but what the next draught, so soon as I popped out of the room, the harpooneer might be standing in the entry, all ready to knock me down!"
> > ];
> >
> > var badwords = ['window', 'chair', 'knockings'];
> >
> >
> > // Fill in function body here
> > var hasBadwords = function (message, index ) {
> > var words = message.split(" ");
> > words.map(function(word,index){
> > if (word == 'window' || word == 'chair' || word == 'knockings'){
> > return true;
> > } else {
> > return false;}
> > });
> >
> > // Tell us what the output is from running this code:
> > console.log(sentences.map(function (sentence, index) {
> > return hasBadwords(sentence) ? index : '';
> > }).join(''));
> >
>
> The error message apparently is "Uncaught SyntaxError: Unexpected end of
> input" and it is used by at least Chrome.
>
> In Firefox the message is different: "SyntaxError: missing } after
> function body".
>
> Do you use an editor with syntax highlighting?

I would still like this to work either way, Thomas' way or my way. It may be bogus but its an exercise of using the map function. I fixed the complaints and it still doesn't work.
var hasBadwords = function (message, index ) {
var bool;
var badwords = ['window', 'chair', 'knockings'];
var words = message.split(" ");
words.map(function (word, index) {
for (var i=0; i< badwords.length; i++){
if (word == badwords) {
bool = true;
}else {
bool = false;}
};
}
);
return bool;
};
// Tell us what the output is from running this code:
console.log(sentences.map(function (sentence, index) {
return hasBadwords(sentence) ? index : '';
}).join(''));</script>
</body></html>

jrough

6/17/2016 8:42:00 PM

0

On Thursday, June 16, 2016 at 3:56:03 AM UTC-7, Thomas 'PointedEars' Lahn wrote:
> Jake Jarvis wrote:
>
> [Pretty-printed code]
>
> > > // Fill in function body here
> > > var hasBadwords = function (message, index) {
> > > var words = message.split(" ");
> > > words.map(function (word,index) {
> > > if (word == 'window' || word == 'chair' || word == 'knockings') {
> > > return true;
> > > } else {
> > > return false;}
> > > }
> > > );
> ^-- }
> > >
> ^-- };
>
> > > // Tell us what the output is from runni ng this code:
> > > console.log(sentences.map(function (sentence, index) {
> > > return hasBadwords(sentence) ? index : '';
> > > }).join(''));
> > >
>
> > The error message apparently is "Uncaught SyntaxError: Unexpected end of
> > input" and it is used by at least Chrome.
> >
> > In Firefox the message is different: "SyntaxError: missing } after
> > function body".
> >
> > Do you use an editor with syntax highlighting?
>
> Wasted effort. The record shows that “JRough” is a script-kiddie: they do
> not program, they copy & pray.
>
> For example, look closely at that code. (Observe the comments, too.) Not
> only is the program logic overly complicated (you could just return the
> result of the boolean expression), it is completely bogus.
>
> Even if you insert the missing “}”s, words.map() and therefore hasBadWords()
> do *nothing of consequence*. The “return” statement returns *from the
> anonymous callback*, not from hasBadWords(), the callback makes no changes,
> and the return value of words.map() is not even used. As a result,
> hasBadWords() would always return “undefined” which is always converted to
> “false”, and so console.log() would always print an Array of empty strings.
>
> Of course, this is *the* use-case for regular expressions instead¹:
>
> var
> badWords = ["window", "chair", "knockings"],
> rxBadWords = new RegExp(
> "\\b(?:"
> + badWords.sort(function (a, b) {
> /*
> * Sort words by length, longest ones first,
> * so that “windows” is preferred over “window”
> */
> return b.length - a.length;
> }).join("|")
> + ")\\b",
> "g");
>
> console.log(sentences.map(function (sentence) {
> return sentence.match(rxBadWords);
> }));
>
> [console.log() will then print “null” if there is no match in a sentence, a
> representation of an Array of matches (with duplicates) otherwise. This can
> be refined to give, e.g. the index of the matches in a sentence, by using
> RegExp.prototype.exec() in a loop instead of String.prototype.match().]
>
I don't know what you mean about refining it to gie the index of the matches in the sentence?
console.log(sentences.map(function (sentence) {
// return sentence.match(rxBadWords);
var words = sentence.split(" ");
for (var i; i< words.length; i++){
rxBadWords.exec(words[i]);
}

}));
thankx, this doens't work,

Thomas 'PointedEars' Lahn

6/17/2016 9:10:00 PM

0

[posted & mailed]

JRough wrote:

[yet another mindbogglingly stupid full-quote following an attribution
novel]

> [â?¦] Thomas 'PointedEars' Lahn wrote:
>> [console.log() will then print â??nullâ? if there is no match in a sentence,
>> a representation of an Array of matches (with duplicates)
>> otherwise. This can be refined to give, e.g. the index of the matches in
>> a sentence, by using RegExp.prototype.exec() in a loop instead of
>> String.prototype.match().]
>
> I don't know what you mean about refining it to gie the index of the
> matches in the sentence?
> console.log(sentences.map(function (sentence) {
> // return sentence.match(rxBadWords);
> var words = sentence.split(" ");
> for (var i; i< words.length; i++){
> rxBadWords.exec(words[i]);
> }
>
> }));
> thankx, this doens't work,

RTFM!

<http://www.catb.org/esr/faqs/smart-question...

F'up2 poster

--
PointedEars
FAQ: <http://PointedEars.... | SVN: <http://PointedEars.de...
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
Please do not cc me. / Bitte keine Kopien per E-Mail.