[Javascript] Submission is evaluated as incorrect while it isn't


  • 0
    L

    When I submit the below code it's marked as incorrect for the following input "a b". The input should be transformed in place to "b a", which does happen as you can see through console.log().

     function moveToTheEnd(str, append) {
         if((str.length === 0) || (append.length === 0)) {
             return str;
         }
         return str.slice(append.length + 1) + ' ' + append;
     }
    var reverseWords = function(str) {
        var remainingChars = str.length;
        var word = '';
        var i = 0;
        while(remainingChars > 0) {
            if(str[i] !== ' ') {
                word += str[i];
            } else {
                str = moveToTheEnd(str, word);
                i = 0;
                word = '';
            }
            i++;
            remainingChars--;
        }
        console.log(str);
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.