Search and Replace Problem

Whats wrong in my code it works fine but not on all inputs.
i am confused…

function myReplace(str, before, after) {
 var arr=[];
  arr=after.split('');
  if(arr[0]===arr[0].toUpperCase)
    {
  arr[0]=arr[0].toLowerCase();
    }
  else
    {
      arr[0]=arr[0].toUpperCase();
    }
    
  after=arr.join('');
  
  
  
  
  return str.replace(before,after);
}
myReplace("This has a spellngi error", "spellngi", "spelling");
//myReplace("A quick brown fox jumped over the lazy dog", "jumped", "leaped"); //works fine

You are forcing the replacement word to uppercase.
http://pythontutor.com/visualize.html

thanx @camperextraordinaire @JohnnyBizzel

i just solved it another way…
function myReplace(str, before1, after1) {
var after=after1.split(’’);
var before=before1.split(’’);

if(before[0]===before[0].toUpperCase())
{
after[0]=after[0].toUpperCase();
}
else
{
after[0]=after[0].toLowerCase();
}
before=before.join(’’);
after=after.join(’’);
return str.replace(before,after);
}
myReplace(“His name is Tom”, “Tom”, “john”);

i am new in programming that’s why my solutions are very simple.