Confirm the ending 123

function confirmEnding(str, target) {
  //var arr=[];
  var arr=str.split(" ");//split the string intoarray
  var teststr=arr[arr.length-1];//taking last string of array as test string
  var teststrarr=[];
  for(var i=teststr.length-1;i>=0;i--){//making array of test string and pushing it into test string array
  var targetarr=target.split('').reverse();//making target string as array and reversing it to make comaprison 
  var teststrarr2=[];
 for(var j=0;j<=targetarr.length-1;j++){//comapring elements of both teststr array and targetarr and put the matched elements in new array teststrarr2
   if(teststrarr2.join('')===targetarr.join('')){//joining both arrays and comparing if compared it will be true otherwise false
     return true;
  return false;

confirmEnding("open sesame", "same");

hey guys in this program I am trying to confirm the ending of a string by comparing it with target string. The program should return true if target string is in the ending of test string otherwise false. My code is not returning true here. I have tried to print the result after ever logic and it seems like there is some problem when I am comparing the elements. Even this thing can be done by substr bultin method but I am trying to use this logic… Please help me identifying the problem Thanks



i think you wanted to check with targetarr[j] not target[j]


you need to call the function with console.log() to print the returning value.

console.log(confirmEnding("open sesame", "same"));


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.