Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
  • 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
@jdtdesigns
easy
John Nunns
@johnnunns
function countWords(str) {
    str.split(' ')
    .reduce(function(result, word){
        var regex = new Regexp(/word/g)

    },{})
    return result
how are we looking? @jdtdesigns
JD Tadlock
@jdtdesigns
@johnnunns That won't work because the regex will only match the word 'word' ;)
you need to create the expression using a string and concat the word into it
new RegExp('expression', 'options')
options would just be 'g' for this one
John Nunns
@johnnunns
g is global?
JD Tadlock
@jdtdesigns
and you don't need the two '/' in the expression string
yes
John Nunns
@johnnunns
I'm really not sure JD I only put /word/g in there because you said haha
JD Tadlock
@jdtdesigns
/(word)/g is the expression to match 'word'
Darth Skywalker
@adityaparab

@johnnunns

g is global?

g is global as in it will match all occurances. Otherwise, Reg Ex only matches first occurance.

John Nunns
@johnnunns
so that would only match one word
and we need it to match all the words
JD Tadlock
@jdtdesigns
/(pickles)/g will match all the 'pickles'
Darth Skywalker
@adityaparab

@johnnunns

so that would only match one word

No. That will only match a string word

it won't match anything else.
John Nunns
@johnnunns
right sorry that's what I mean
so what regexp matches all the words together that match?
Darth Skywalker
@adityaparab
the right regex for that is \w or [A-Za-z]
the w in \w should be small
capital W like \W will match nonword characters
John Nunns
@johnnunns
var regex = new Regexp(\w, 'g')
JD Tadlock
@jdtdesigns
so to create an expression using the RegExp object, you would do it like this
var regex = new RegExp('(' + word + ')', 'g')
John Nunns
@johnnunns
aha...
JD Tadlock
@jdtdesigns
which outputs /(myword)/g
John Nunns
@johnnunns
what's the reason for ' ' around ()?
JD Tadlock
@jdtdesigns
you can't place the argument into the expression without string concatenation
otherwise it will just match the exact word you put in
that's the only reason you need to use the RegExp object
otherwise you could just use straight regex
John Nunns
@johnnunns
function countWords(str) {
    str.split(' ')
    .reduce(function(result, word){
        var regex = new Regexp( '(' + word + ')', 'g')

    },{})
    return result
}
fair enough!
JD Tadlock
@jdtdesigns
lookin good
but
John Nunns
@johnnunns
ok so now we need to set the counter
JD Tadlock
@jdtdesigns
result needs to be inide the callback
inside*
John Nunns
@johnnunns
so I don't need to return result outside?
JD Tadlock
@jdtdesigns
result refers to object you are building inside the callback
it needs to be returned inside
John Nunns
@johnnunns
I thought it automatically did that in reduce I guess..
alright so the counter. result[word] = regex something something something
JD Tadlock
@jdtdesigns
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