how to use a loop to cap the last letter of a string
let x = "hello my name is chrono"
let reg = x.split(' ')
for(let i = 0; i < reg.length; i++) {
reg[i] = reg[i][0].toUpperCase() + reg[i].substr(1)
} reg.join(" ") console.log(reg) >>>>>>>>>>>>>>>>>>>>>>>>>>>>>> The code above is to change the first letter of the string to uppercase. The next lesson is to change the last letter instead of the first Please help
reg[i] = reg[i][0].toUpperCase() + reg[i].substr(1)
} reg.join(" ") console.log(reg) >>>>>>>>>>>>>>>>>>>>>>>>>>>>>> The code above is to change the first letter of the string to uppercase. The next lesson is to change the last letter instead of the first Please help
8 Replies
that isn't changing the first letter of the input string, but the first letter of every word in the input string, is that what you want?
also, what are you stuck with? We're here to help, but not to write your homework assignments for you, and this really sounds like it's a homework assignment
the code I pasted there was to cap the first letter of every words in the string, I want now to make all the last letter of every word cap
also thanks for responding
you can use this to get the last character of a string https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice
I can use regex and other method but the exercise is to use loop
is there a way I can use loop
to target the last letter?
I think trying to figure that out is part of the assignment, right?
ya
part one was to are that first part
I was thinking for the loop
for(let i = reg.length - 1; i < 0; i++)
but its not caping anything with that
you've got the i<0 the wrong way around 🙂
i
needs to be greater than zero, not lessi tried that too but it still didn’t work, maybe i need to push() the value
let x = "hello my name is chrono"
let reg = x.split(' ')
for(let i = 0; i < reg.length; i++) {
reg[i] = reg[i].substr(0, reg[i].length - 1) + reg[i][reg[i].length - 1].toUpperCase()
} reg.join(" ") console.log(reg) I figured it out lol
} reg.join(" ") console.log(reg) I figured it out lol