Basic Algorithm Scripting: Confirm the Ending**


I am getting this errors
*confirmEnding(“Connor”, “n”) should return false. *confirmEnding("Open sesame", "pen") should return false.
*confirmEnding(“If you want to save our world, you must hurry. We dont know how much longer we can withstand the nothing”, “mountain”)` should return false.

function confirmEnding(str, target) {
   let strPost=str.localeCompare(target)



                 if(strPost==-1){
                    return true; 
                 }else if(strPost==1){
                      return false;
                 }else if (strPost==0){
                        return true;
                 }
                 
         

confirmEnding("Connor", "n");

I believe localeCompare() won’t work in this challenge and any test that passes when using it is simply false positive. Right now you are simply comparing both strings and you are getting result from localeCompare() depending which string is alphabetically first.

1 Like

It is done. Thanks!

function confirmEnding(str, target) {
let indTarget=str.indexOf(target);
let startTarget=str.length-target.length;
let endStr=str.substring(startTarget);

console.log(endStr)
     
     if (indTarget>=0){
           
           if(target==endStr){
           
               return true;
             
               
            }else{
               return false;  
            } 
         
     }else{
          return false;
     } 
                        
}

confirmEnding("Open sesame", "pen");