SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict
    Join Date
    Dec 2010
    Posts
    217
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    The variable seems right, but...

    Hi,

    The following loop works:

    Code:
    <html>
    <body>
    <script type="text/javascript">
    var i=0;
    for (i=0;i<=5;i++)
    {
    document.write("The number is " + i);
    document.write("<br />");
    }
    </script>
    </body>
    </html>
    But the following doesn't:

    Code:
    <html>
    <body>
    <script type="text/javascript">
    var i=0;
    var x="i=0;i<=5;i++"
    for (x)
    {
    document.write("The number is " + i);
    document.write("<br />");
    }
    </script>
    </body>
    </html>
    I'd just like to create a simple variable.
    Please bear with me as I'm a newbie in JavaScript and let me know what I'm missing.

    Many thanks in advance!
    Rain Lover

  2. #2
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    187
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your second example contains variable x that stores a string value.

    String value can be anything, and Javascript interpreter will NOT interpret it as valid code that could be executed. If interpreters would interpret anything that might be code withing string value, we'd have a mess

    Question is, why do you want to store that expression in a variable and use it in the loop? Is it because you're learning or your design imposes that you require such a feature?

  3. #3
    SitePoint Addict
    Join Date
    Dec 2010
    Posts
    217
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by furicane View Post
    Question is, why do you want to store that expression in a variable and use it in the loop? Is it because you're learning or your design imposes that you require such a feature?
    Thanks for the answer! Actually I'm creating a drop-down menu in a Google gadget that lets users to switch between two loops -- one of them is a loop that puts the results in an ascending order and the other one is a loop that arranges them in a reverse order. The only code difference between these loops is what you see in parentheses:
    (i=0;i<=5;i++) vs. (i=5;i>=0;i--)

    The problem is I'm not allowed to use a long expression as a variable in a Google gadget and that's why I decided to define variables for them.

    Any workaround?

  4. #4
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    187
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, nothing imposes that you need to use 2 for loops. In the loop, you're incrementing value of variable i. Who says you can't decrement value of variable j in the same loop and achieve reverse effect at the same time?

  5. #5
    SitePoint Addict
    Join Date
    Dec 2010
    Posts
    217
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Let me provide my sample Google gadget:

    Code:
    <?xml version="1.0" encoding="UTF-8" ?> 
    <Module> 
    <ModulePrefs title="Sample Gadget" /> 
    <UserPref name="order" 
              display_name="Results Order" 
              default_value="i = 0; i <= 5; i++" datatype="enum"> 
    <EnumValue value="i = 0; i <= 5; i++" display_value="Ascending"/> 
    <EnumValue value="i = 5; i >= 0; i--" display_value="Descending"/> 
    </UserPref> 
    <Content type="html"><![CDATA[ 
    <script type="text/javascript"> 
    var i=0; 
    for (__UP_order__) 
    { 
    document.write("The number is " + i); 
    document.write("<br />"); 
    } 
    
    </script> 
    ]]></Content> 
    </Module>
    It doesn't work because of the tags <> (they're not supported), and that's why I tried to define a variable for the EnumValue value.

  6. #6
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Unless Google is parsing that JS and doing some replacement of __UP_order__ for the string in the <EnumValue/> tag, then the for (__UP_order__) bit looks pretty dodgy. You need semicolons for a "normal" for loop, and the "in" operator for a for-in loop.

    Unfortunately I know nothing about Google gadgets - only that your javascript is wrong!


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
  •