Announcement

Collapse
No announcement yet.

Strings vs Variables

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Strings vs Variables

    Why does

    var hNQ=0;
    s="nq h4,1";
    hNQ=(high (-5,1,s)*1+high (-1,1,s)*1+high (-2,1,s)*1+high (-3,1,s)*1+high (-4,1,s)*1)/5;

    yield a number and

    var hNQ=0;
    s="nq h4,1";
    hNQ=(high (-5,1,s)+high (-1,1,s)+high (-2,1,s)+high (-3,1,s)+high (-4,1,s))/5;

    yield a string?

  • #2
    Dave,

    I observed the problem. I copied your code and ran it. Below is the code:


    PHP Code:
                var hNQ=0;
                
    s="nq h4,1";
                
    debugPrintln("high (-5,1,s) = "+(high (-5,1,s+1)+" high (-1,1,s) = "+high (-1,1,s)+" high (-2,1,s) = "+high (-2,1,s)+" high (-3,1,s) = "+high (-3,1,s)+" high (-4,1,s) = "+high (-4,1,s));
                
    hNQ=(high (-5,1,s)*1+high (-1,1,s)*1+high (-2,1,s)*1+high (-3,1,s)*1+high (-4,1,s)*1)/5
    Interestingly, everything looked alright, except where I added 1 to the 'high (-5,1,s)' value.
    Obviously, you had figured that out already because you had multiplied by a 1 to get a value. I think this goes back to Javascripts use of untyped variables. This particular use of the high operand resulted in the return of a value. When multiplied by a 1, the compiler figured out it was numerical( multiplication is not used when manipulating text). When 1 was added to it, it was enterpreted as a text value, since a '+' is used to add text together and numbers. This is Similar to my debugPrintLn statement.

    This is review for you, but see the
    link. This is not real specific but it talks about Javascript being untyped.

    Hopefully this will make sense. Here is the output of the debug window. Notice the value associated with high (-5,1,s), it is text.

    Regards,

    Steve
    Attached Files
    Last edited by ; 12-16-2003, 07:24 PM.

    Comment


    • #3
      Dave,

      This related link also discusses the automatic data conversions which are performed in Javascript.

      Regards,

      Comment


      • #4
        Hello Guys,

        Steve, great info here. I have a little more insight to add to this topic. The return values from the EFS functions getValue(), open(), high(), low() and close() can be a single number or an array of numbers.

        If the second parameter (nNumBars) is specified in the call, then the functions return an array. Even if you only specify 1 for nNumBars. So in David's example, you can multiply high(-5, 1, s) *1 and the JavaScript engine knows to convert that to a number. However, if nNumBars is 2 or greater, the return array will have an equivalent size. In this case, multiplying an array of size 2 or greater by 1 generates, NaN, or "Not a Number" because the engine can't make a valid conversion.

        If the nNumBars parameter is omitted then the return value will be a single number. You can do high(-5) or high(-5, s) and get a single number for the return value.

        Run the following code example. I set up two arrays. One with a size of 1 and the other with a size of 3. You'll see what happens when you try to multiply each by 1. I also put David's code examples in so you can see what happening there as well. Without *1, we also get NaN.

        PHP Code:
        function preMain() {
            
        setPriceStudy(true);
            
        setStudyTitle(" test ");
            
        setShowCursorLabel(false);
        }

        var 
        aArray1 = new Array(1);
        aArray1[0] = 25;
        var 
        aArray2 = new Array(3);
        aArray2[0] = 20;
        aArray2[0] = 30;
        aArray2[0] = 40;

        var 
        true;
        debugClear();

        function 
        main() {
            if (
        == true && getCurrentBarIndex() == -1) {
                
        debugPrintln("aArray1 *1 = " aArray1 1);
                
        debugPrintln("aArray2 *1 = " aArray2 1);
                
        debugPrintln("*****************************");
                
                var 
        hNQ=0;
                var 
        s="nq h4,1";
                
        hNQ=(high(-5,1,s) + high(-1,1,s) + high(-2,1,s) + high(-3,1,s) + high(-4,1,s))/5;
                
        debugPrintln("hNQ without *1 = " hNQ);
                
                
        hNQ=(high(-5,1,s)*high(-1,1,s)*high(-2,1,s)*high(-3,1,s)*high(-4,1,s)*1)/5;
                
        debugPrintln("hNQ with *1 = " hNQ);
                
                
        false;
            }
            return;

        Jason K.
        Project Manager
        eSignal - an Interactive Data company

        EFS KnowledgeBase
        JavaScript for EFS Video Series
        EFS Beginner Tutorial Series
        EFS Glossary
        Custom EFS Development Policy

        New User Orientation

        Comment


        • #5
          Jason,

          So what you are saying, in effect, is that the return from high(-5) or high(-5, s) is a typed numerical variable. and a 1 dimensional array, w=new Array(1), when used as w[0] is a typed variable, but when used without the brackets (w) is untyped?

          Regards,

          Comment


          • #6
            Hello Steve,

            I'm not totally sure your use of "typed" and "untyped" here is the proper way to describe your examples. Typed and untyped, to me, is more of a description of the language itself. JavaScript is an untyped language, which means that variables don't have to have a specific data type declared. JavaScript variables can be recycled essentially to store different data types throughout your code. You don't even have to var variables in JavaScript either, which is a downfall in my opinion. You can mistype a variable name in your code and the syntax checker won’t catch it. Anyway, Typed languages require that the data type is declared when initializing a variable and can only store that declared data type throughout the code, I think. I'm not an expert in Typed languages, so somebody correct me if I'm wrong.

            The important thing to remember here is what is returned from the EFS functions based on the parameters passed.

            w = high(-5)
            w is a number.

            w = high(-5, s)
            w is a number.

            w = high(-5, 1, s)
            w is an array of size 1. w can be referenced as w*1 or w[0].

            w = high(-5, 2, s)
            w is an array of size 2. w can only be referenced as w[0] and w[1].

            w = new Array(1)
            w is an Array of size 1.
            This could be referenced as w*1 or w[0], because JavaScript can convert the w*1 to a number.

            w = new Array(2)
            w is an Array.
            The elements of this array can only be referenced as w[0] or w[1].
            w*1 in this case generates NaN because the engine can't do this conversion of an array with a size greater than 1.
            Jason K.
            Project Manager
            eSignal - an Interactive Data company

            EFS KnowledgeBase
            JavaScript for EFS Video Series
            EFS Beginner Tutorial Series
            EFS Glossary
            Custom EFS Development Policy

            New User Orientation

            Comment


            • #7
              Good addition for the help file

              Jason,

              That is a pretty good explanation for me, I had a fairly good handle on most of what you discussed, but your concise explanation puts the whole issue into a nice package on the subject (yes, even a guy with a Commodore 64 would understand). You should save your last post and get it into the efs help file. Excellent job!!

              Regards,

              Comment


              • #8
                Re: Good addition for the help file

                Originally posted by stevehare2003
                Jason,

                ... You should save your last post and get it into the efs help file. Excellent job!!

                Regards,
                I second this motion. Your Help System is typical of many programs these days. IOW, adequate at best.

                Comment

                Working...
                X