Jump to content

Question on JS Type Conversion lesson in tutorial


sixwi

Recommended Posts

Hello - first time posting.

I've searched the forums to see if anyone else had the same question, with no luck. I'm going through the lessons in the tutorial but I can't figure out what's going on in part of the JS Type Conversion lesson. I'll link to the full lesson here, but below is the part I'm having trouble with: (mainly the function)

Quote

 

The constructor property returns the constructor function for all JavaScript variables. You can check the constructor property to find out if an object is an Array (contains the word "Array"):

Example code:

<p>This "home made" isArray() function returns true when used on an array:</p>

<p id="demo"></p>

<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = isArray(fruits);

function isArray(myArray) {
  return myArray.constructor.toString().indexOf("Array") > -1;
}

</script>

 

I have no idea what's happening in the function... could someone be kind enough to break it down in detail for me? I understand how arrays work. But the function here has me stumped.

Thanks so much

 

 

Edited by sixwi
Clarity
Link to comment
Share on other sites

  • sixwi changed the title to Question on JS Type Conversion lesson in tutorial
2 hours ago, sixwi said:

Hello - first time posting.

I've searched the forums to see if anyone else had the same question, with no luck. I'm going through the lessons in the tutorial but I can't figure out what's going on in part of the JS Type Conversion lesson. I'll link to the full lesson here, but below is the part I'm having trouble with: (mainly the function)

<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = isArray(fruits);

function isArray(myArray) {
  return myArray.constructor.toString().indexOf("Array") > -1;
}

</script>

I have no idea what's happening in the function... could someone be kind enough to break it down in detail for me? I understand how arrays work. But the function here has me stumped.

Thanks so much

 

 

NEVERMIND - I figured it out.... but I can't figure out how to delete this topic!

I'll go ahead and explain the function anyway.

<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = isArray(fruits);

function isArray(myArray) { 
  return myArray.constructor.toString().indexOf("Array") > -1; 
}

</script>

 

Breakdown:

The parameter (myArray) will be the "fruits" array.
myArray.constructor would return "function Array() { [native code] }".
Which gets converted to a string with the toString() method.
Then the indexOf("Array") would be 9. (would be -1 if the word "Array" wasn't found)
It then compares the result and since 9 > -1 the return is "true".

 

If I can't get this deleted then I hope it helps someone.
Thanks again

Edited by sixwi
Edited for clarity
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...