Results 1 to 6 of 6

Thread: a slice of javascript theory?

  1. #1
    Join Date
    Dec 2006
    Beans
    596

    a slice of javascript theory?

    Given the array:
    Code:
    var a=[4,5,6,7,8,9];
    Why does...

    instance A:
    Code:
    x=4;
    document.write(x);
    if (a.indexOf(x)!=-1) {document.write("yep!");}
    else {document.write("nope!");}
    ...yield yep!

    instance B:
    Code:
    x=a[0];
    document.write(x);
    if (a.indexOf(x)!=-1) {document.write("yep!");}
    else {document.write("nope!");}
    ...yield yep!, but...

    instance C:
    Code:
    x=a.slice(0,1);
    document.write(x);
    if (a.indexOf(x)!=-1) {document.write("yep!");}
    else {document.write("nope!");}
    ...yield nope!

    When, for all instances (A,B,C)...
    Code:
    document.write(x);
    ...yields 4

    Thanks for any guidance!
    Last edited by maclenin; March 21st, 2013 at 09:12 PM.

  2. #2
    Join Date
    Oct 2007
    Beans
    1,914
    Distro
    Lubuntu 12.10 Quantal Quetzal

    Re: a slice of javascript theory?

    Dear OP, you've got to admit that your question looks a bit like homework. Thus, I shall not give more than a hint: "types". There are many sources reading more about types in JavaScript.

  3. #3
    Join Date
    Jul 2008
    Location
    The Left Coast of the USA
    Beans
    Hidden!
    Distro
    Kubuntu

    Re: a slice of javascript theory?

    maclenin's post is not a homework question and the thread has been restored.

    Cheers!
    The Community Java Wiki * The Community ATI Driver Wiki * Find what you need in the Ubuntu Community wikis

    And seeing the terror on the faces of the people, QIII did speak to them, saying "Fear not the Terminal, for it is a road both swift and secure to the Great Mysteries of Linux without passing through the dangers of The Land of Ten Thousand Competing Desktop Environments."

  4. #4
    iMac71 is offline Gee! These Aren't Roasted!
    Join Date
    Dec 2012
    Beans
    172

    Re: a slice of javascript theory?

    Code:
    var a=[4,5,6,7,8,9];
    xB=a[0]; // x of instance B
    xC=a.slice(0,1); // x of instance C
    document.write(xB + "<br>");
    document.write(a.indexOf(xB) + "<br>");
    document.write(xC + "<br>");
    document.write(a.indexOf(xC) + "<br>");
    document.write((xB===xC) + "<br>");
    the result of test in last line shows that xB and xC aren't of the same type: xB is a number, while xC is an array (see here: http://www.w3schools.com/jsref/jsref_slice_array.asp).
    Last edited by iMac71; March 23rd, 2013 at 08:12 AM.

  5. #5
    Join Date
    Dec 2006
    Beans
    596

    Re: a slice of javascript theory?

    QIII!

    Thanks, again.

    iMac71!

    Thanks for your test scenario. I hear you.

    With your lessons in mind (and via jsbin's lab)...

    Code:
    var a=[4,5,6,7,8,9];
    var b=[0,4,8,12,16,20];
    
    xC=a.slice(0,1);
    xD=a.slice(0,1);
    
    y=b.indexOf(xC);
    y1=b.indexOf(xC[0]);
    
    z=b[xC];
    z1=b[xC[0]];
    
    document.write("xC=array <i>"+typeof(xC)+"</i> <b>xC</b> contains <i>"+typeof(xC[0])+"</i>(s) <b>"+xC+"</b> from array <i>"+typeof(a)+"</i> <b>a</b><br>");
    document.write("xD=array <i>"+typeof(xD)+"</i> <b>xD</b> contains <i>"+typeof(xD[0])+"</i>(s) <b>"+xD+"</b> from array <i>"+typeof(a)+"</i> <b>a</b><br>");
    document.write("<br>");
    document.write("y=the indexOf array <i>"+typeof(xC)+"</i> <b>xC</b> in array <i>"+typeof(b)+"</i> <b>b</b> is <b>"+y+"</b><br>");
    document.write("y1=the indexOf <i>"+typeof(xC[0])+"</i> <b>"+xC+"</b> in array <i>"+typeof(b)+"</i> <b>b</b> is <b>"+y1+"</b><br>");
    document.write("<br>");
    document.write("z=the <i>"+typeof(z)+"</i> <b>"+z+"</b> is in position "+xC+"  in array <i>"+typeof(b)+"</i> <b>b</b><br>");
    document.write("z1=the <i>"+typeof(z1)+"</i> <b>"+z1+"</b> is in position "+xC[0]+"  in array <i>"+typeof(b)+"</i> <b>b</b><br>");
    document.write("<br>");
    document.write("Should the result of \"(xC===xD)\" be <b>"+(xC===xD)+"</b>?<br>");
    document.write("<br>");
    document.write(xC+"<br>");
    document.write(xD+"<br>");
    ...these questions emerge:

    1. for y, why is xC treated as an object, whereas for z, xC seems to resolve to the number 4?

    2. for y1 (and z1), it seems treating xC as an object with [reference] to a specific element allows xC to be evaluated by indexOf.

    3. xC===xD is still false, should this be so? Perhaps, yes? See the bottom of this page.

    4. xC and xD BOTH resolve to the number (?) 4 via document.write().

    Thanks, again, for your help!

  6. #6
    iMac71 is offline Gee! These Aren't Roasted!
    Join Date
    Dec 2012
    Beans
    172

    Re: a slice of javascript theory?

    Quote Originally Posted by maclenin View Post
    1. for y, why is xC treated as an object, whereas for z, xC seems to resolve to the number 4?
    because for z you use the square brackets instead of indexOf(), hence [xC] becomes a kind of reference instead of a single element array
    Quote Originally Posted by maclenin View Post
    2. for y1 (and z1), it seems treating xC as an object with [reference] to a specific element allows xC to be evaluated by indexOf.
    xC[0] is the first element of xC, i.e. 4, which is a number: hence xC[0] can be used for finding its value into array b[] with indexOf
    Quote Originally Posted by maclenin View Post
    3. xC===xD is still false, should this be so? Perhaps, yes? See the bottom of this page.
    Perhaps the reason could be that two variables of identical type and identical content occupy different memory places, hence the '===' operator gives false as the result of comparison between them.
    Quote Originally Posted by maclenin View Post
    4. xC and xD BOTH resolve to the number (?) 4 via document.write().

    Thanks, again, for your help!
    xC and xD are single element arrays, while xC[0] and xD[0] are numbers
    Last edited by iMac71; March 24th, 2013 at 06:41 AM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •