Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #30663
| Path | csiph.com!weretis.net!feeder4.news.weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail |
|---|---|
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
| Newsgroups | comp.lang.javascript |
| Subject | Re: some logic error on a map function |
| Date | Thu, 16 Jun 2016 12:55:57 +0200 |
| Organization | PointedEars Software (PES) |
| Lines | 108 |
| Message-ID | <1898608.dsHlmFUGLj@PointedEars.de> (permalink) |
| References | <e46e5762-3e70-475d-93e7-d4ed92e9d3e7@googlegroups.com> <njtm7u$ug4$1@dont-email.me> |
| Reply-To | Thomas 'PointedEars' Lahn <cljs@PointedEars.de> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="UTF-8" |
| Content-Transfer-Encoding | 8Bit |
| X-Trace | solani.org 1466074558 26152 eJwVwoENwDAIA7CXoA0ZnDPo8v8J1SzHpnMeMIhQyE20EHKvsAP2ekGu8kpND9Snav7+AWkXBkIQhA== (16 Jun 2016 10:55:58 GMT) |
| X-Complaints-To | abuse@news.solani.org |
| NNTP-Posting-Date | Thu, 16 Jun 2016 10:55:58 +0000 (UTC) |
| User-Agent | KNode/4.14.2 |
| X-User-ID | eJwFwQkBwDAIA0BLfElBTunAv4Tdwal8JwgGFuvhCxmN1kmRld1ravS0azPBOuSTSmlq2amcyg/96oJq/QMv6hR6 |
| Cancel-Lock | sha1:VZ4wul1URNa2VgjEMVInS/H6R0o= |
| X-NNTP-Posting-Host | eJwFwQEBwCAMAzBLMNptl1Pg9S+BhCtnnkIyQdMVZdftRBlq/TNyD9QH33PCUlCmhri0Gw8ilRGR |
| Xref | csiph.com comp.lang.javascript:30663 |
Show key headers only | View raw
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/regexp>
[2] <http://www.ecma-international.org/ecma-262/6.0/#sec-get-regexp.prototype.flags>
[3] <http://www.ecma-international.org/ecma-262/6.0/#sec-regexpbuiltinexec>
[4] <https://en.wikipedia.org/wiki/Google_Chrome_release_history>
--
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
some logic error on a map function JRough <janis.rough@gmail.com> - 2016-06-15 20:23 -0700
Re: some logic error on a map function Jake Jarvis <pig_in_shoes@yahoo.com> - 2016-06-16 09:58 +0200
Re: some logic error on a map function "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-06-16 12:47 +0200
Re: some logic error on a map function Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-06-16 12:55 +0200
Re: some logic error on a map function Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-06-16 14:08 +0200
Re: some logic error on a map function JRough <janis.rough@gmail.com> - 2016-06-16 11:04 -0700
Re: some logic error on a map function JRough <janis.rough@gmail.com> - 2016-06-16 11:04 -0700
Re: some logic error on a map function JRough <janis.rough@gmail.com> - 2016-06-17 13:30 -0700
Re: some logic error on a map function JRough <janis.rough@gmail.com> - 2016-06-17 13:41 -0700
Re: some logic error on a map function Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-06-17 23:10 +0200
Re: some logic error on a map function JRough <janis.rough@gmail.com> - 2016-06-17 13:31 -0700
Re: some logic error on a map function Scott Sauyet <scott@sauyet.com> - 2016-07-03 03:07 +0000
Re: some logic error on a map function JRough <janis.rough@gmail.com> - 2016-07-09 11:38 -0700
csiph-web