Algorithm script if statement not returning true even though it should

Tell us what’s happening:
After looking at the solution I realized there was a way of doing this which is 100x easier. However, I feel like my code should meet all the requirements and be a viable solution but it just isn’t working.

In the if statement:

if(targetLetters == end[0])

I am comparing whether the array of split characters from the target parameter on the function is equal to the last characters from the array of the split str parameter(through my code I made the last characters equal to the length of the number of characters the target parameter has). Both variables in the if statement both equal [ ‘a’, ‘c’, ‘t’, ‘i’, ‘o’, ‘n’ ], which can be seen using the console.log()'s that I put there but it still doesn’t return true.

   **Your code so far**

function confirmEnding(str, target) {
let letters = str.split('')
let targetLetters = target.split('')
let targetLength = targetLetters.length
let lastLetters = letters.length-targetLength
let end = []

if(letters[lastLetters] == targetLetters){
  return true
}else if(targetLength > 1){
for (let i = 0; i < lastLetters; i++){
  letters.shift()
}
end.push(letters);
console.log(targetLetters)
console.log(end[0])
if(targetLetters == end[0]){
return true
}else{
  return false
}
}else{
  return false
}
};

confirmEnding("Abstraction", "action");
   **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36

Challenge: Confirm the Ending

Link to the challenge:

These are the only requirements that did not pass out of the 11 requirements

confirmEnding(“Congratulation”, “on”) should return true.

confirmEnding(“He has to give me a new name”, “name”) should return true.

confirmEnding(“Open sesame”, “same”) should return true.

confirmEnding(“Abstraction”, “action”) should return true.

Since only the true tests are failing, you need to look at where you determine true, which is here:

This array comparison is not determining if the two arrays are the same size, shape, and contain the same elements in the same order. You have to do that. There is a good deal of ink spilled on stack overflow and the web in general about array comparison. Your comparison is returning false for everything except 1 character strings that match (from the first conditional). It doesn’t matter that the two arrays in the comparison are the same size, shape, and have the same elements in the same order; they are different objects and therefore different. You could, however, put them back together as a string and then try comparing them.

2 Likes

Thank you so much! I assigned both variables in the comparison each to a new variable to take the values out of array form like:

let fLetter = targetLetters.join()
let fEnd = end[0].join()

and then replaced the variables in the comparison with the new ones and it worked perfectly. I appreciate your help!

When dealing with Strings, use Regex. I didn’t use Regex for this challenge myself, but I understand later on that it might’ve been the simplest solution.

I used Regex on 3 of the 5 projects required for the certification. Regex .test() can be powerful when combined with higher order functions such as filter, map, and most importantly: reduce. You’ll learn these later on, so keep regex in mind.

1 Like

I tried using regex in previous challenges in this section and I didn’t have too much luck. I think it’s just my lack of understanding of regex so it would probably be in my best interest to go through that section again. Thanks for the advice!

1 Like

Regex takes practice due to how different it can be from what beginners are used to. I suggest trying to apply it to any program that requires a tons of strict string comparison, and working it out as you go along.

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