Cannot read property 'length' of undefined

Im stuck on this error message, would anyone be able to help me with this please.

The challenge: Complete the solution so that it returns true if the first argument(string) passed in ends with the 2nd argument (also a string).

My code:

function solution(str, ending){
  var arrayStr = str.split('').reverse();
  var arrayEnding = ending.split('').reverse();
  if (arrayStr.length !== arrayEnding.length) {
    var n = arrayEnding.length;
    var arrayStr1 = arrayStr.slice(0, n);
  for (i = 0; i < arrayEnding.length; i++) {
    for (j = 0; j < arrayStr1.length; j++) {
      if (arrayEnding[i] == arrayStr1[j]) {
      } else if (arrayEnding[i] !== arrayStr1[j]) {
        return false;
    } return true;

Can you please post the actual code instead of a screenshot, so we can test the code?

Thank you.

To enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

Please use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks are not single quotes.


Thank you for informing me, I have edited my post :slight_smile:

Add the following console.log statement at the beginning of the function and check your browser’s console, to see where it is failing.

console.log(str, ending);

You need to validate your algorithm is correct, because this code logic is not close. You only need one for loop (not a nested for loop).