Looking for an efficient way to convert a specific letter in a string to uppercase? Let's explore different methods:
Suppose we have the string:
let str = "Dwightschrute";
One way to achieve this is by slicing the string and then updating the desired index:
let str = "Dwightschrute";
let a = str.slice(0, 6);
console.log(a); // Dwight
let b = (str.slice(6, 7)).toUpperCase();
console.log(b); // S
let c = str.slice(7);
console.log(c);
console.log(a + b + c); // DwightSchrute
Alternatively, you can convert the string into an array, update the desired element, and join it back into a string:
let str = "Dwightschrute";
str = [...str];
str[6] = str[6].toUpperCase();
console.log(str.join('')); // DwightSchrute
Although these methods work, they may seem complex. Are there other simpler ways to accomplish the same task? Feel free to explore and share!