Confirm the ending - not sure why my code will not pass one of the tests

Confirm the ending - not sure why my code will not pass one of the tests
0.0 0

#1

Hello,

I’ve check the solution in the hint section, much shorter and cleaner than mind but I would still like to understand why what I’ve done doesn’t work …

When test with “Open sesame”, “same” it say false instead of true

Thanks a lot for your help :slight_smile:


function confirmEnding(str, target) {   
  // "Never give up and good luck will find you."   
  // -- Falcor
  var swap = [];  //use to reverse the string 
  for (i = str.length; i >= 0; i--) {
    swap.push(str[i]);   
  }
  swap.shift();   
  swap = swap.join(""); //swap contain the reverse string "i checked"
  
  var sizeoftarget = []; //to compare target with an identical length string
  for (i = 0; i < target.length; i++) {   
    sizeoftarget[i] = swap[i];   
  }
  sizeoftarget = sizeoftarget.join("");   
    if (target === sizeoftarget) {   
      return true;
    }   
    else {   
      return false;   
    }   
}   

confirmEnding("Bastian", "name"); 

Confirm the ending
#2

I moved your question to a new topic, because it was asking a different question. Always start a new thread if your question pertains to a specific problem with your code.

To answer your question: In the above example, target is “same”. You have a variable called sizeoftarget which contains a string (which is the first 4 letters of swap which is a the reverse of the original string). So, in the above example, sizeoftarget is “emas”, so because they are not equal, your function returns false.


#3

Ok here is what is wrong with your logic to solving this problem. If I am correct, your logic is basically reversing the given str and seeing if the beginning of the reversed str is equivalent to the target string that you are given. All the test cases that are supposed to result in false are completed for you because your logic isn’t properly checking for the correct cases which will give you true.

For the test case, confirmEnding("He has to give me a new name", "name"), swap is equivalent to "eman wen a em evig ot sah eH" and so “eman” !== “name”.


#4

Hello,

Thanks a lot to both of you, I’m new so I didn’t knew I was supposed to create a new topic.
(can you explain how people see each new topic and answer so fast ? it’s really bluffing !)

Plus thanks to both of you I’ve understood that I also needed to reverse the target, I did and it work !
Thanks for helping me, I was running short on idea …

Another question by the way, I have no notification on my mail when I get an answer and I don’t have the reflex to loggin in the forum everytime (for an obsur reason, it’s not the same password so I have to log github to log the forum …). Do you know if it’s possible to activate an email alert or something like that ?

Thanks a lot <3


#5

At the bottom of a topic, you will see a button above the Suggested Topics section. If you click on it, you will see 4 options which are explained. If you select Watching, you will be notified of every new reply to the topic.

image


#6

Thanks a lot I havn’t noticed :slight_smile: