0

It was supposed to capitalize first characters of each string but it doesn't. Please tell me how I can solve this problem.

    let array = ["string1", "string2", "string3", "string4"];
    let btn = document.getElementById("btn");

    array.forEach(function (item) {
      item.charAt(0).toUpperCase();
    })

    btn.addEventListener("click", function () {
      document.getElementById("p").innerHTML = array.join(" ")
    })```
Larian
  • 3
  • 2

1 Answers1

-1

You are getting the first letter and converting it to uppercase, but you aren't doing anything with it.

You need to concatenate the rest of the string to the first character, then reassign back.

let array = ["string1", "string2", "string3", "string4"];

array.forEach(function(item, i) {
  array[i] = item.charAt(0).toUpperCase() + item.substring(1)
})

console.log(array.join(" "))
Spectric
  • 30,714
  • 6
  • 20
  • 43