Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #30828

Re: some logic error on a map function

Newsgroups comp.lang.javascript
Date 2016-07-09 11:38 -0700
References <e46e5762-3e70-475d-93e7-d4ed92e9d3e7@googlegroups.com> <njtm7u$ug4$1@dont-email.me> <113419b8-abab-4608-ae31-b0f9eee5d9a4@googlegroups.com> <nl9vh7$5oo$1@dont-email.me>
Message-ID <1dbd9a1e-8079-4bb7-916e-e6c31967276e@googlegroups.com> (permalink)
Subject Re: some logic error on a map function
From JRough <janis.rough@gmail.com>

Show all headers | View raw


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.

Back to comp.lang.javascript | Previous | NextPrevious in thread | Find similar | Unroll thread


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