Jump to content

Question on JS Type Conversion lesson in tutorial


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 post
Share on other sites
  • sixwi changed the title to Question on JS Type Conversion lesson in tutorial
Posted (edited)
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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...