Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #30660 > unrolled thread
| Started by | JRough <janis.rough@gmail.com> |
|---|---|
| First post | 2016-06-15 20:23 -0700 |
| Last post | 2016-07-09 11:38 -0700 |
| Articles | 13 — 5 participants |
Back to article view | Back to comp.lang.javascript
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
| From | JRough <janis.rough@gmail.com> |
|---|---|
| Date | 2016-06-15 20:23 -0700 |
| Subject | some logic error on a map function |
| Message-ID | <e46e5762-3e70-475d-93e7-d4ed92e9d3e7@googlegroups.com> |
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(''));
[toc] | [next] | [standalone]
| From | Jake Jarvis <pig_in_shoes@yahoo.com> |
|---|---|
| Date | 2016-06-16 09:58 +0200 |
| Message-ID | <njtm7u$ug4$1@dont-email.me> |
| In reply to | #30660 |
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?
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2016-06-16 12:47 +0200 |
| Message-ID | <XnsA629821F76E90eejj99@194.109.6.166> |
| In reply to | #30661 |
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)
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2016-06-16 12:55 +0200 |
| Message-ID | <1898608.dsHlmFUGLj@PointedEars.de> |
| In reply to | #30661 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2016-06-16 14:08 +0200 |
| Message-ID | <5489482.L0yonRzWca@PointedEars.de> |
| In reply to | #30663 |
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.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.
[toc] | [prev] | [next] | [standalone]
| From | JRough <janis.rough@gmail.com> |
|---|---|
| Date | 2016-06-16 11:04 -0700 |
| Message-ID | <7962101a-1eae-4b4a-8ff3-4124531742fc@googlegroups.com> |
| In reply to | #30663 |
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/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.
Okay, thanks, the logic is better to have a regular expression instead of a second map.
[toc] | [prev] | [next] | [standalone]
| From | JRough <janis.rough@gmail.com> |
|---|---|
| Date | 2016-06-16 11:04 -0700 |
| Message-ID | <4495805d-5db3-4634-a98c-c764984d0b7c@googlegroups.com> |
| In reply to | #30663 |
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/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.
thanks,
[toc] | [prev] | [next] | [standalone]
| From | JRough <janis.rough@gmail.com> |
|---|---|
| Date | 2016-06-17 13:30 -0700 |
| Message-ID | <6f299c27-9f75-427c-a0d1-0ab941305abc@googlegroups.com> |
| In reply to | #30663 |
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,
[toc] | [prev] | [next] | [standalone]
| From | JRough <janis.rough@gmail.com> |
|---|---|
| Date | 2016-06-17 13:41 -0700 |
| Message-ID | <707175fc-85a5-44c8-8d81-08b2329594b9@googlegroups.com> |
| In reply to | #30663 |
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,
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2016-06-17 23:10 +0200 |
| Message-ID | <2033046.3EF9NDWLXO@PointedEars.de> |
| In reply to | #30674 |
[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-questions.html>
F'up2 poster
--
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.
[toc] | [prev] | [next] | [standalone]
| From | JRough <janis.rough@gmail.com> |
|---|---|
| Date | 2016-06-17 13:31 -0700 |
| Message-ID | <113419b8-abab-4608-ae31-b0f9eee5d9a4@googlegroups.com> |
| In reply to | #30661 |
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>
[toc] | [prev] | [next] | [standalone]
| From | Scott Sauyet <scott@sauyet.com> |
|---|---|
| Date | 2016-07-03 03:07 +0000 |
| Message-ID | <nl9vh7$5oo$1@dont-email.me> |
| In reply to | #30673 |
JRough wrote:
> Jake Jarvis wrote:
>> JRough wrote
>>> I get the error : unexpected end of input, there is some logic
>>> error before it joins the words.
>>>
[ ... note: sentences here reduced from original ...]
>>> var sentences = [
>>> "I now took ... with a chair.",
>>> "But it was ... no yoking them.",
>>> "I then placed ... settle down in.",
>>> "But I ... the window, ... the night.",
>>> "The devil ... violent knockings? ... dismissed it.",
>>> "For who could tell ... knock me down!"
>>> ];
>>>
>>> var badwords = ['window', 'chair', 'knockings'];
>>>
>>>
>>> // Fill in function body here
>>> var hasBadwords = function (message, index) {
[ ... implementation from OP removed ... ]
>>> };
>>> // 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.
Does this have anything to do with Jake's comment? Please, you've been
around here plenty long enough to know how to reduce quotes to the
relevant minimum, to ask questions better than "it doesn't work --
help!", to note actual attempts to help and distinguish them from noisy
attempts to prove a point. Please contribute to the dialogue.
> It may be bogus but its an exercise of using the map function.
The outer wrapper is certainly an appropriate use of `map`. But for the
function you've presumably been asked to write ("Fill in function body
here") `map` makes little sense. `map` is about applying a function to
the value(s) of a container and returning a new container of the same
shape. In the case of `Array.prototype.map`, that container is always an
array, but the concept is more general. [1] However, all you want out of
the function is a boolean that reports whether any of the bad words are
included in the message. `map` makes no sense. After a little
manipulation of the message string, you will have an array of words to
test, but you do not want back an array of that same size as a result.
There are other functions more related to what you're doing here.
Perhaps most fundamental is `reduce`, which fits the bill in one way, as
the point of `reduce` is to take a container and fold it down into a
single value, which sounds like what you want to do. But `reduce` is
bringing out the big guns. It's overkill for this simple problem.
`filter` is somewhat more helpful, as it would allow you to find all the
words which match a given predicate. You *could* use this, and then
check if the resulting length is positive. But that is still overkill,
because you are only asked to find the sentences that contain the
forbidden words, not to document all the places in those sentences where
the words occur.
There would also be ways to do this with `find`. But I won't go into
them, because there is clearly a right function for this: `some`. All
you want to know is whether there is some word in the list contained in
the excluded set. If you find one, you're done, and shouldn't need to
test the rest. While `find` would similarly stop when it finds a value,
`find` returns the value; `some` returns a boolean to say that it has
found something; that latter is precisely what's desired here. `some`
is also supported by more implementations than `find`.
So here is one solution:
var hasBadwords = function (message) {
var words = message.replace(/[^\w\s]/g, '').split(/\s+/);
return words.some(function(word) {
return badwords.includes(word);
});
};
(Note that I dropped the unnecessary `index` parameter here.)
`includes` may not be supported where you need it, so you could use this
work-around:
var hasBadwords = function (message) {
var words = message.replace(/[^\w\s]/g, '').split(/\s+/);
return words.some(function(word) {
return badwords.indexOf(word) > -1;
});
};
I would suggest that this is not wonderful code, as this function is not
referentially transparent, depending on it does on the global `badwords`
list. I would prefer to see that as a parameter to this, or to see a
function which accepted that and returned the equivalent of this function.
This is not to say that there is anything wrong with the regex solution
proposed. To my eyes this looks much closer to what's asked for in the
problem. However, if this is supposed to be an exercise in how to use
`map`, then I think it's poorly designed.
[1]: https://github.com/fantasyland/fantasy-land#functor
-- Scott
[toc] | [prev] | [next] | [standalone]
| From | JRough <janis.rough@gmail.com> |
|---|---|
| Date | 2016-07-09 11:38 -0700 |
| Message-ID | <1dbd9a1e-8079-4bb7-916e-e6c31967276e@googlegroups.com> |
| In reply to | #30812 |
On Saturday, July 2, 2016 at 8:07:25 PM UTC-7, Scott Sauyet wrote:
> JRough wrote:
> > Jake Jarvis wrote:
> >> JRough wrote
>
> >>> I get the error : unexpected end of input, there is some logic
> >>> error before it joins the words.
> >>>
>
> [ ... note: sentences here reduced from original ...]
>
> >>> var sentences = [
> >>> "I now took ... with a chair.",
> >>> "But it was ... no yoking them.",
> >>> "I then placed ... settle down in.",
> >>> "But I ... the window, ... the night.",
> >>> "The devil ... violent knockings? ... dismissed it.",
> >>> "For who could tell ... knock me down!"
> >>> ];
> >>>
> >>> var badwords = ['window', 'chair', 'knockings'];
> >>>
> >>>
> >>> // Fill in function body here
> >>> var hasBadwords = function (message, index) {
>
> [ ... implementation from OP removed ... ]
>
> >>> };
> >>> // 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.
>
> Does this have anything to do with Jake's comment? Please, you've been
> around here plenty long enough to know how to reduce quotes to the
> relevant minimum, to ask questions better than "it doesn't work --
> help!", to note actual attempts to help and distinguish them from noisy
> attempts to prove a point. Please contribute to the dialogue.
>
>
>
> > It may be bogus but its an exercise of using the map function.
>
> The outer wrapper is certainly an appropriate use of `map`. But for the
> function you've presumably been asked to write ("Fill in function body
> here") `map` makes little sense. `map` is about applying a function to
> the value(s) of a container and returning a new container of the same
> shape. In the case of `Array.prototype.map`, that container is always an
> array, but the concept is more general. [1] However, all you want out of
> the function is a boolean that reports whether any of the bad words are
> included in the message. `map` makes no sense. After a little
> manipulation of the message string, you will have an array of words to
> test, but you do not want back an array of that same size as a result.
>
> There are other functions more related to what you're doing here.
> Perhaps most fundamental is `reduce`, which fits the bill in one way, as
> the point of `reduce` is to take a container and fold it down into a
> single value, which sounds like what you want to do. But `reduce` is
> bringing out the big guns. It's overkill for this simple problem.
>
> `filter` is somewhat more helpful, as it would allow you to find all the
> words which match a given predicate. You *could* use this, and then
> check if the resulting length is positive. But that is still overkill,
> because you are only asked to find the sentences that contain the
> forbidden words, not to document all the places in those sentences where
> the words occur.
>
> There would also be ways to do this with `find`. But I won't go into
> them, because there is clearly a right function for this: `some`. All
> you want to know is whether there is some word in the list contained in
> the excluded set. If you find one, you're done, and shouldn't need to
> test the rest. While `find` would similarly stop when it finds a value,
> `find` returns the value; `some` returns a boolean to say that it has
> found something; that latter is precisely what's desired here. `some`
> is also supported by more implementations than `find`.
>
> So here is one solution:
>
> var hasBadwords = function (message) {
> var words = message.replace(/[^\w\s]/g, '').split(/\s+/);
> return words.some(function(word) {
> return badwords.includes(word);
> });
> };
>
> (Note that I dropped the unnecessary `index` parameter here.)
>
> `includes` may not be supported where you need it, so you could use this
> work-around:
>
> var hasBadwords = function (message) {
> var words = message.replace(/[^\w\s]/g, '').split(/\s+/);
> return words.some(function(word) {
> return badwords.indexOf(word) > -1;
> });
> };
>
> I would suggest that this is not wonderful code, as this function is not
> referentially transparent, depending on it does on the global `badwords`
> list. I would prefer to see that as a parameter to this, or to see a
> function which accepted that and returned the equivalent of this function.
>
> This is not to say that there is anything wrong with the regex solution
> proposed. To my eyes this looks much closer to what's asked for in the
> problem. However, if this is supposed to be an exercise in how to use
> `map`, then I think it's poorly designed.
>
>
> [1]: https://github.com/fantasyland/fantasy-land#functor
>
> -- Scott
I agree, it was poorly designed therefore it made it difficult. I also agree the regex was super clean and nice example of using prototype. Just couldn't figure out how to finish it.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web