In: Computer Science
Write a function in JAVASCRIPT that accepts two arguments (a string array and a character). The function will check each character of the strings in the array and removes the character, regardless of case. If the first letter of the string is removed the next letter must be capitalized. Your function would return the result as one string, where each element of array is separated by comma. E.G. function([“John”,”Hannah”,”Saham”], “h”); // returns ‘Jon,Anna,Saam
Use of any built in string function or any built in array function is not alllowed,
any help would be much appreciated.
//function to remove given character from array of strings
//input: Array of strings, character to remove
//output: String as specified per requirements
function removeChar(StringArray,Ch){
var n2,result;
var n1 = Ch.charCodeAt(0);
if(n1 >= 97) n2 = n1 - 32;
else n2 = n1 + 32;
var length = 0;
var stringLength,temp;
var finalResult = "";
//first find the length of array of strings
while (StringArray[length] !== undefined)
length++;
//Iterate through each string from array
for(var i = 0; i< length;i++){
//first find length of each string
stringLength = 0;
result = "";
//each string is assigned to temp
temp = StringArray[i];
//stringLength has length of the string
while (temp[stringLength] !== undefined) stringLength++;
for(var j = 0;j<stringLength;j++)
{
if(temp.charCodeAt(j) != n1 && temp.charCodeAt(j) != n2 )
{
if(result == '' && temp.charCodeAt(j) >= 97) result +=String.fromCharCode(temp.charCodeAt(j) - 32);
else result = result + temp[j];
}
}
if(i != length - 1) finalResult = finalResult + result + ',';
else finalResult = finalResult + result;
}
return finalResult;
}
console.log(removeChar(["John","Hannah","Saham"],"h"))
//output:
**************************************************************************
Please give an upvote,as it matters to me a lot :)
Got any doubts? Feel free to shoot them in the comment
section
**************************************************************************