Make sure to test all your answers in the console.
var s = "cars , trucks,bikes"
. So there are words, and they are separated by commas, but there might be one or more spaces (or possibly even tabs) on either side of those commas. You should not make any assumptions about how many spaces there are. You can assume that there are no spaces at the very beginning or at the very end.
r
should we use, so that s.split(r)
returns the array ["cars", "trucks", "bikes"]
? Make sure that your regular expression removes those spaces too, the resulting array should contain only the words without any spaces. s.match(/../g)
would result in the same array. Notice that we are using a “g” so that the regular expression matches as many times as it can. So the regular expression that you provide should be the one that matches just a single word. s.substr(i, j)
in this specific example in order to get back the string "trucks"
. What would i
, j
be? s.substring(i, j)
. average
that takes as an argument an array arr
. It should return the average of the values in the array: So it adds all the values up, then divides by the array length. Do not forget the return
statement and to test your function. howManyWords
that takes as an argument a string s
and returns the number of “words” in s
.