Path: csiph.com!weretis.net!feeder4.news.weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail From: Thomas 'PointedEars' Lahn 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> References: Reply-To: Thomas 'PointedEars' Lahn 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 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] [2] [3] [4] -- PointedEars FAQ: | SVN: Twitter: @PointedEars2 | ES Matrix: Please do not cc me. / Bitte keine Kopien per E-Mail.