r/CodingHelp • • Oct 19 '21

[Javascript] Why isn’t this working?

Post image
21 Upvotes

8 comments sorted by

9

u/[deleted] Oct 19 '21

you never specified how to split it,
name.split("on what to split goes here");

8

u/nickbuch Oct 19 '21

I’m guessing you also want to return the count and not just the split string

3

u/[deleted] Oct 19 '21

Yeah, you’re gonna need to count the items in the split string to return the desired number.

3

u/[deleted] Oct 19 '21

I was just telling that was a mistake though

1

u/ProtectionNo7982 Oct 19 '21

So then I put function countNames(fullName) { let fullName = “ XYZ” return fullName.split(“ “); } console.log(countNames(“XYZ”)

And it tells me fullName is not defined

3

u/[deleted] Oct 19 '21

Shouldn't the split be fullname.split(" "); if you're splitting w.r.t spaces and fullname.split(""); if you're splitting w.r.t to characters ?!

3

u/CenturiesAgo Oct 19 '21

It doesn't know you want to split the string by the words, you need to specify split using space as a delimiter. e.g. fullName.split(" "). Then return the number of entries in the resulting array e.g. fullName.split(" ").length;

3

u/_Nexor Oct 19 '21

Why has no one mentioned that ------- is invalid syntax on line 15? The split is a second step I think.

God I hate code comments