Search and Replace - answer works but not accepted


I’ve written a very basic answer for the ‘Search and Replace’ exercise and it returns the correct answers, please see the code below. However it is not being accepted as a correct answer.

Could anyone tell me why that it the case?

Any help would be appreciated :slight_smile:

var newStr = [];
function myReplace(str, before, after) {
        if(before[0] === before[0].toUpperCase()){
            var capAfter = after.replace(after[0], after[0].toUpperCase());
            toUpperCase(str, before, capAfter);
        } else {
            toUpperCase(str, before, after);
        function toUpperCase(x, y, z){
            var split = x.split(' ');
            for(var i = 0; i < split.length; i++){
                newStr.push(split[i].replace(y, z));
        return newStr.join(" ");
myReplace("Let us go to the store", "store", "mall");

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36.

Link to the challenge:

Your code works, just don’t declare newStr globally , put it inside your myReplace() function

note: it doesn’t work when declared globally not because it is syntactically wrong or you made a mistake, but because of the way the test module uses the global name space, or something to that effect.

Thank you @Dereje1 ! :smiley: