Where communities thrive

  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
  • Aug 17 09:32
    User @bjorno43 unbanned @linkin-park
  • Jun 20 21:19
    @bjorno43 banned @shenerd140
  • Apr 25 17:13
    @mstellaluna banned @cmal
  • Jan 08 22:05
    @mstellaluna banned @dertiuss323
  • Nov 07 2018 04:42
    User @texas2010 unbanned @ows-ali
  • Nov 02 2018 16:25
    @texas2010 banned @ows-ali
  • Oct 12 2018 05:50
    @bjorno43 banned @NACH74
  • Oct 05 2018 23:02
    @mstellaluna banned @JomoPipi
  • Sep 16 2018 12:21
    @bjorno43 banned @yash-kedia
  • Sep 16 2018 12:16
    @bjorno43 banned @vnikifirov
  • Sep 05 2018 08:12
    User @bjorno43 unbanned @androuino
  • Sep 05 2018 07:38
    @bjorno43 banned @androuino
  • Aug 23 2018 16:57
    User @bjorno43 unbanned @rahuldkjain
  • Aug 23 2018 16:23
    @bjorno43 banned @rahuldkjain
  • Jul 29 2018 14:13
    User @bjorno43 unbanned @jkyereh
  • Jul 29 2018 01:00
    @bjorno43 banned @jkyereh
  • Jul 10 2018 22:09
    @bjorno43 banned @manafn
  • Jul 06 2018 15:20
    @texas2010 banned @imlegend19
  • Jul 03 2018 12:28
    @bjorno43 banned @vbvmatta
  • Jun 29 2018 13:54
    @bjorno43 banned @OGTechnoBoy
JD Tadlock
result[word] = ....
you're building result lol
result starts as {}
and inside the callback you build it with each value in the array passed to it
so you split the string and reduce is iterating over each word
each word is passed to the callback
and you do something with the word and add it to the result object
John Nunns
so the values that we want to put inside it is the regex amounts
JD Tadlock
that's where .match() comes into play
you can use the regex you created to match the amount of those words ;)
John Nunns
haha I thought we matched them already?
JD Tadlock
the regex is just regex lol
it needs to be used somehow
John Nunns
ok fair enough. so how about result[word] = str.match(regex)?
How do you create html elements using jquery? I found multiple ways on google, not sure which one is right.
John Nunns
it's definitely wrong after submitting haha, but a good try right
man I hope my learning curve is at least on par with everyone else, this stuff is hard... @jdtdesigns
JD Tadlock
@johnnunns str.match(regex).length should work ;)
John Nunns
ok let me show you the whole thing then maybe error is somewhere else
JD Tadlock
you need to return result below the .match
John Nunns
function countWords(str) {
    str.split(' ')
    .reduce(function(result, word){
        var regex = new Regexp( '(' + word + ')', 'g');
        result[word] = str.match(regex).length
      return result;


countWords("hey whta updsls a dgfg s")
JD Tadlock
return str.split(' ').....
John Nunns
@fortMaximus I think that there is no wrong way. Use .append or .html or whatever fits your needs
John Nunns
Regexp is not defined
JD Tadlock
@fortMaximus var link = $('</ li>');
it's RegExp not Regexp
John Nunns
passing 1 of 3!
pretty good! :) haha
@jdtdesigns then how do I add it to html file/code?
John Nunns
weird though man it's working just fine. But I'm failing at :
JD Tadlock
@fortMaximus sorry, that's 2.1
in version 3 just use $('h1');
@fortMaximus what are you trying to do?
@johnnunns Show me some test cases they are passing to your function
John Nunns
found the error not sure how to fix
'ask a bunch get a bunch' returns - { ask: 1, a: 3, bunch: 2, get: 1 }
so it's returning 'a' 3 times
JD Tadlock
cause a is a word
and it's in there 3 times :D
o wait
2 times