Confirm the Ending 1234

Confirm the Ending 1234
0

#1

Tell us what’s happening: Hey guys I was trying to do this excersize confirm the ending of string by giving target string and testing string. I cant figure out whats the problem in my code why it is not giving true for given result.

Your code so far

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

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

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36.

Link to the challenge:


#2

I am having trouble figuring out why you are doing certain things in your code. You are doing so many things which are not needed for this challenge (i.e. reversing an array).

Anyway, the instructions ask you to use one of the JavaScript substring methods instead to solve this challenge.

You do not need arrays of any kind to solve this challenge, so no need to split anything or join anything back together. Read about substring and substr functions and you should be able to solve it fairly easy.

Try using one of the above functions in your solution and if you get stuck, let us know.