Apply the map() function to the variable str so that every second word in the string is converted to uppercase. in javascript.
str = "hello my name is jon and i live in. canada."
>Solution :
"hello my name is jon and i live in. canada."
.split(" ")
.map((word, idx) => idx % 2 === 0 ? word : word.toUpperCase())
.join(" ")
The trick is:
- Split by spaces.
- Map with index, but only use
toUpperCase()ifidx % 2 !== 0. - Then
joinback with spaces.