I am facing a challenge where I must remove a letter from each element in an array and alter the first letter if it is at index 0. My current strategy involves the following code:
function capital(arr, char){
let str = "";
let result = "";
for (let i = 0; i < arr.length; i++){
str = arr[i] + (i < arr.length - 1 ? ",": "");;
for (let j = 0; j < str.length; j++){
if (str[j] === char){
result += "";
if (str[j] === char){
result += (j === 0? "A": "");
}
else {
result += str[j];
}
}
}
console.log(result);
}
capital(["doritos","sandking","bandana", "demand"], "d");
This program aims to eliminate all instances of the letter 'd' in the strings and change the first letter to 'A' if 'd' is at index 0.
The current output is
Aoritos,sanking,banana,Aeman
However, the desired result should be
Aritos,sanking,banana,Aman
The condition is that built-in functions cannot be used, and the program needs to be case insensitive. I can work on addressing these concerns by tweaking the code and adding conditional statements, but I need help specifically with ensuring the modification to index 0. Any assistance would be greatly appreciated. Thank you!