Results 1 to 7 of 7

Thread: What does the "?" mean in this?

  1. #1

    Thread Starter
    Hyperactive Member Al Smith's Avatar
    Join Date
    May 1999
    Location
    Marcellus, MI. USA
    Posts
    330

    What does the "?" mean in this?

    Hi,
    In the code below, what does the question mark do in the lines var keyCode... and var filter...?

    Code:
    var isNN = (navigator.appName.indexOf("Netscape")!=-1);
    
    function autoTab(input,len, e) {
       var keyCode = (isNN) ? e.which : e.keyCode; 
       var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
       if(input.value.length >= len && !containsElement(filter,keyCode)) {
       input.value = input.value.slice(0, len);
       input.form[(getIndex(input)+1) % input.form.length].focus();
       }
    Thanks,
    Al.
    A computer is a tool, not a toy.

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    I can answer part of it.... For starters, it's JAvascript -- wasn't sure you got that or not.
    "var isNN" and "var keycode" and "var filter" are variable declarations.
    The funk stuff after the "=" is something known as "regular expresssions".... I believe in this case that it is checking for valid data entries.
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    var keyCode = (isNN) ? e.which : e.keyCode;


    It's the same as saying:

    If (isNN) is true, then keyCOde = e.which
    otherwise
    if (isNN) is false, then keyCode = e.keycode

    It's just a shortcut method for that


    HTH

  4. #4

    Thread Starter
    Hyperactive Member Al Smith's Avatar
    Join Date
    May 1999
    Location
    Marcellus, MI. USA
    Posts
    330
    Hi,
    Thanks for the reply.
    I kind of understand JavaScript, e.g. var's, but I'm unfamiliar with a question mark in the variable declaration. I found something that mentioned a "switch" or "condition" but this didn't mean anything to me.
    The above code came from a JavaScript AutoTab function. Here's the complete code.
    Code:
    <SCRIPT LANGUAGE="JavaScript">
    
    var isNN = (navigator.appName.indexOf("Netscape")!=-1);
    
    function autoTab(input,len, e) {
       var keyCode = (isNN) ? e.which : e.keyCode; 
       var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
       if(input.value.length >= len && !containsElement(filter,keyCode)) {
       input.value = input.value.slice(0, len);
       input.form[(getIndex(input)+1) % input.form.length].focus();
       }
    
       function containsElement(arr, ele) {
       var found = false, index = 0;
       while(!found && index < arr.length)
       if(arr[index] == ele)
       found = true;
       else
       index++;
       return found;
       }
    
       function getIndex(input) {
       var index = -1, i = 0, found = false;
       while (i < input.form.length && index == -1)
       if (input.form[i] == input)index = i;
       else i++;
       return index;
       }
    return true;
    }
    
    </script>
    The HTML uses onKeyUp="return autoTab(this, 2, event); in the input box.

    Thanks,
    Al.
    A computer is a tool, not a toy.

  5. #5

    Thread Starter
    Hyperactive Member Al Smith's Avatar
    Join Date
    May 1999
    Location
    Marcellus, MI. USA
    Posts
    330
    Thanks mendhak.
    Al.
    A computer is a tool, not a toy.

  6. #6
    Frenzied Member
    Join Date
    Aug 2000
    Location
    O!
    Posts
    1,177
    Originally posted by Al Smith
    I kind of understand JavaScript, e.g. var's, but I'm unfamiliar with a question mark in the variable declaration. I found something that mentioned a "switch" or "condition" but this didn't mean anything to me.
    You can declare and initialize variables in the same statement.
    var keyCode = (isNN) ? e.which : e.keyCode; is just a shortcut for:
    var keyCode;
    keyCode = (isNN) ? e.which : e.keyCode;

    isNN is the condition.

    BTW, this type of statement can also be found in Java and C. It is sometimes called a ternary, most likely due to the 3 expressions that make up the entire expression: (expr1) ? expr2 : expr3.

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    BTW, there are no regular expressions in that code.

    ?: is the "conditional operator", and it is a ternary operator. This means it operates on three operands as opposed to binary (2 operands) or unary (1 operand) operators.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

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



Click Here to Expand Forum to Full Width