sande
August 21, 2021, 4:27am
1
this code is working when i console log and the output is perfect, but when i click on run the tests the code is not working. ANY HELP
function titleCase(str) {
let split = str.toLowerCase().split(" ")
let newword = ""
for(let i=0; i<split.length; i++){
newword += split[i][0].toUpperCase()+split[i].slice(1)+" "
}
return newword;
}
titleCase("I'm a little tea pot");
Challenge: Title Case a Sentence
Link to the challenge:
Your output has an extra space at the end.
I suggest storing your words in an array, and then returning the result of the join()
method.
The join() method creates and
returns a new string by concatenating all of the elements in an array (or an array-like
object), separated by commas or a specified separator string. If the array has
only one item, then that item will be returned...
sande
August 21, 2021, 5:14am
3
so i added these lines
let join = newword.split(' ') return join.join(' ')
instead of return newword
but still not working
With your original code, you could try the trim method here
sande:
return newword;
sande
August 21, 2021, 5:59am
5
i do not know how to use trim
Trying looking at the docs and see how you can use it in your return statement here.
sande:
return newword;
ILM
August 21, 2021, 10:45am
7
what’s your whole code now?
sande
August 21, 2021, 10:54am
8
function titleCase(str) {
let split = str.toLowerCase().split(" ")
let newword = ""
for(let i=0; i<split.length; i++){
newword += split[i][0].toUpperCase()+split[i].slice(1)+" "
}
let join = newword.split(' ') return join.join(' ')
instead of return newword
}
titleCase("I'm a little tea pot");
ILM
August 21, 2021, 10:57am
9
Why don’t you create a second array with your loop instead of forcing join
in there like that?
this does exactly the same as
sande:
return newword
It would be helpful to you if you check for which string this function fails, then check if you can fix that. It can be dauting to leave a solution in favour for something new, but it will actually help. Try storing the words in an array could actually be very useful to push each word into an empty array after turning the first character to Uppercase, then use the newword.join(’ ')
system
Closed
February 20, 2022, 2:49am
11
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.