Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

some logic error on a map function

41 views
Skip to first unread message

JRough

unread,
Jun 15, 2016, 11:23:55 PM6/15/16
to
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(''));

Jake Jarvis

unread,
Jun 16, 2016, 3:59:00 AM6/16/16
to
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.

unread,
Jun 16, 2016, 6:47:31 AM6/16/16
to
Jake Jarvis <pig_in...@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

unread,
Jun 16, 2016, 6:56:03 AM6/16/16
to
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.

Thomas 'PointedEars' Lahn

unread,
Jun 16, 2016, 8:08:26 AM6/16/16
to
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.

JRough

unread,
Jun 16, 2016, 2:04:33 PM6/16/16
to
Okay, thanks, the logic is better to have a regular expression instead of a second map.

JRough

unread,
Jun 16, 2016, 2:04:51 PM6/16/16
to
On Thursday, June 16, 2016 at 3:56:03 AM UTC-7, Thomas 'PointedEars' Lahn wrote:
thanks,
Message has been deleted

JRough

unread,
Jun 17, 2016, 4:32:03 PM6/17/16
to
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

unread,
Jun 17, 2016, 4:41:55 PM6/17/16
to
On Thursday, June 16, 2016 at 3:56:03 AM UTC-7, Thomas 'PointedEars' Lahn wrote:
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

unread,
Jun 17, 2016, 5:10:34 PM6/17/16
to
[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

Scott Sauyet

unread,
Jul 2, 2016, 11:07:25 PM7/2/16
to
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

JRough

unread,
Jul 9, 2016, 2:39:00 PM7/9/16
to
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.
0 new messages