Why is this line of coding wrong?

At this point I know enough JavaScript to make a big mess… at times it works, and lots of times I keep running into “can’t do that like this”…

Now… why is this wrong? My console says “reverse is not a function” … but I did see it kind of the same way at MDN docs…

function countup(n) {
if (n < 1) {
return ;
} else {
const countArray = countup(n - 1);
const array1 = countArray.push(n);
const array2 = array1.reverse();
return array2;
}
}
console.log(countup(5));

mdn reference here:

reverse is indeed not a function defined on the sort of thing array1 is. It isn’t an array! What does push return?

Whenever you see that message 9 out of 10 times it is because you are calling a prototype method on the wrong data type, which means it won’t have that method on it. In your case, you are calling reverse on a number.

typeof [].length
'number'

the push() methold seems to change the length property of the object…

Now… this below is the good working sample:

function countup(n) {
  if (n < 1) {
    return [];
  } else {
    const countArray = countup(n - 1);
    countArray.push(n);
    return countArray;
  }
}
console.log(countup(5));

the next code below DOESN’T work… obviously using an extra variable in the same line of code is wrong…

function countup(n) {
  if (n < 1) {
    return [];
  } else {
    const countArray = countup(n - 1);
    const array1 = countArray.push(n);

    return array1;
  }
}
console.log(countup(5));
1 Like

That’s true, but you didn’t say what the return value of push is. Is it an array?

1 Like

So, the data type for the push() method should be working for an array.

Apparently array1 is not an array… which means that I can’t be putting variables all that much or it will change the types and stuff won’t work… and as soon as I get array1 out of the code, the code works fine…

Which also makes me think… what is the data type of countArray and why does it work with push() if I don’t put a variable in front of it?

This are some open questions that I need to search a bit more, or else I am going to keep mixing types and methods a lot more…

Just read the docs. I don’t know why you are just assuming the return type.

Now, you are certainly not the first to think push returns the new array, but that doesn’t mean your assumption is correct.

I don’t know what is the data type of the value from push()

It does say it adds to the the length of an array.

But I think the problem comes from using a variable in front of the push() method… that variable is now changing the types… I think I need to do a bit more reading of types to understand why this is happening:

This works:

const countArray = countup(n - 1);
countArray.push(n);
return countArray;

But this doesn’t work:

const countArray = countup(n - 1);
const array1 = countArray.push(n);
return array1;

You are massively overthinking this. Please, for your own sanity, read the first sentence of this link

Return value

The new length property of the object upon which the method was called.

This means that your array1 is not an array but instead is a _______

The type depends on what you are working with… what is important is the specific case you are working with, and surely it won’t return an array. If I put a variable in front of push(), then the variable will change the original type to something else… in this case it would change the original array to a number when calling the recursion, basically, you can’t push a number into a number.

The push() methods even when puts out a different type value of countArray still works without the variable, because it is pushing the number into the array, so when the recursion is called it still maintain the array type with the added push item into the array.

Things with different value types can still interact… but why was the code breaking just by using an extra variable? Now I know.

No. The type of countArray is not changed by calling push.

Can you finish this sentence? Please. The documentation we linked and quoted says that push returns the new length of the array… What type of thing is a length? A length isn’t an array. A length isn’t a string. A length isn’t a boolean. A length isn’t an object. A length is a ______

Because the length is a ____, that means it isn’t an array and the returned value cannot use push .

Compare with this code which is between your two

function countup(n) {
  if (n < 1) {
    return [];
  } else {
    const countArray = countup(n - 1);
    const newLength = countArray.push(n);
    console.log(typeof(countArray), typeof(newLength));
    return countArray;
  }
}
console.log(countup(5));

Hi there!

I am sorry, I just can’t say with 100% certainty what is the type of the output for push()… if my life would depend on it I would call a number as I said before… I would call it a number because the array that we are working with is made of numbers… the MDN documents only says that it extends the property of the object, and I don’t know what would it translate to…

On the other hand, I know push() is not outputting an array or new array, but it is adding information to an array, that would be enough to cause the error I see and change the code.

Yeah, it’s a length which is a number. And a number isn’t an array.

That’s not why it’s a number…

Where do you see that?

Did you not see this sentence in the documentation for push under the Return Value section? I’m not sure we’re reading the same page?

Yes, I saw it… I am not sure what that means exactly for the output of push().

push() could insert a string, or a number, or maybe something else? , but I can’t say what would be the length property type for the array…

Lets back way, way up.

    const countArray = countup(n - 1);
    const newLength = countArray.push(n);

Here, on the first line you create an array and call it countArray.

On the second line, you call push on this new array.

What does push do? Well, lets carefully read the documentation.

The push() method of Array instances adds the specified elements to the end of an array and returns the new length of the array.

Ok, looks like two things happen.

First, n is going to be added to the end of the countArray.

Then, the new length of the array is returned.

A length is going to be something like 2 or 5 or something like that. Those look like numbers to me.

But, we don’t have to guess. We could either run some code and check, or read the documentation:

The length data property of an Array instance represents the number of elements in that array. The value is an unsigned, 32-bit integer that is always numerically greater than the highest index in the array.

Looks like the length is a number, for sure.

You cannot call push on a number, or reverse.

But in your original code you were trying to call an array method on the return value from calling push:

1 Like

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