Un-woof
The words of a sentence
In this exercise we split up a single string into an array of words. e.g.
'This is four words'
will become
['This', 'is', 'four', 'words']
Use
split
to break the string up into words.
function splitIntoWords(sentence) {
// Write your code here
}
tests({
'splitIntoWords is a function': (t) => {
t.isEqual(typeof splitIntoWords, 'function')
},
'splitIntoWords(\'This is four words\') returns [\'This\', \'is\', \'four\', \'words\']': (t) => {
t.isDeeplyEqual(splitIntoWords('This is four words'), ['This', 'is', 'four', 'words'])
}
})
Picking out specific words
Once we have an array, we can use
array.filter(...)
to build a new array without the elements that we don't want. We keep the items we want by returning true
and remove those we don't by returning false
.
Use filter
to remove the word 'woof'
, we can use !=
to filter out
words in the array that are 'woof'
e.g. noWoofs(['Hello', 'woof', 'World'])
should return ['Hello', 'World']
function noWoofs(words) {
// return true or false depending on each word
return words.filter(word => { return true })
}
tests({
'noWoofs is a function': (t) => {
t.isEqual(typeof noWoofs, 'function')
},
'noWoofs(["Hello", "woof", "World"]) returns ["Hello", "World"]': (t) => {
t.isDeeplyEqual(noWoofs(["Hello", "woof", "World"]), ["Hello", "World"])
}
})
Ignoring upper or lower case
Use filter
to remove the word 'woof'
, and also any other case of woof like
'Woof'
, 'WOOF'
, or 'wOoF'
you can use toLowerCase
to make each word lower case before comparing it to 'woof'
function noWoofs(words) {
// return true or false depending on each word
return words.filter(word => true)
}
tests({
'noWoofs is a function': (t) => {
t.isEqual(typeof noWoofs, 'function')
},
'noWoofs(["Hello", "woof", "World"]) returns ["Hello", "World"]': (t) => {
t.isDeeplyEqual(noWoofs(["Hello", "woof", "World"]), ["Hello", "World"])
},
'noWoofs(["Hi", "WOOF", "folks"]) returns ["Hi", "folks"]': (t) => {
t.isDeeplyEqual(noWoofs(["Hi", "WOOF", "folks"]), ["Hi", "folks"])
},
'noWoofs(["wOoF", "Howdy", "WoOf", "pardner"]) returns ["Howdy", "pardner"]': (t) => {
t.isDeeplyEqual(noWoofs(["wOoF", "Howdy", "WoOf", "pardner"]), ["Howdy", "pardner"])
},
})
Joining our words together into a sentence
Now we want to join our words
back together into one string with words.join(...)
We'll pass a single space ' '
as the joiner so that the words read together as a sentence.
function joinWords(words) {
// join `words` together to make a sentence
return words
}
tests({
'joinWords([\'one\', \'two\', \'three\'])': (assert) => {
assert.isEqual(joinWords(['one', 'two', 'three']), 'one two three')
},
})
Everything, Everywhere, All at once
Now we'll put all these skills together. Define a function called unwoof
(we've started you off with some code)
that takes one parameter called sentence. This sentence is a string.
- Break the string into words
- Remove the words that are "woof" (in any combination of upper and lower case)
- Join those words back together in a new sentence
- Return that new sentence
e.g. unwoof("Help! woof I'm being WoOf attacked by WOOF a chihuahua")
should return
"Help! I'm being attacked by a chihuahua"
function unwoof(sentence) {
// remove the woofs
return sentence
}
tests({
'unwoof("Help! woof I\'m being WoOf attacked by WOOF a chihuahua")': (assert) => {
assert.isEqual(unwoof("Help! woof I\'m being WoOf attacked by WOOF a chihuahua"), 'Help! I\'m being attacked by a chihuahua')
},
'unwoof(\'Hello World\')': (assert) => {
assert.isEqual(unwoof('Hello World'), 'Hello World')
}
})