www.webdeveloper.com
Results 1 to 5 of 5

Thread: how could I store multiple values returned by eval() ?

Hybrid View

  1. #1
    Join Date
    Jan 2014
    Posts
    7

    how could I store multiple values returned by eval() ?

    I want to store the value of every variable ikj in
    ikj=(eval(read[i]));

    where read[i] is " i=20" ,"j=30" , "k=i*j"

    and then display the value of k . And I am even successful though, but if there's

    " i=20" ,"j=30" , "k=i*j", "m=30", "n=20", "p=m*n"

    then only result of p gets displayed when I want value of even k to be displayed, I dropped the idea of dynamically creating variables as it would be hectic for very long codes. Thank you

  2. #2
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    255
    If eval function successfully parses the string then it returns the value of the last expression or statement in the string or undefined if the last expression or statement had no value. If you want to store the result of each expression then you can create a loop, store each expression inside of an array and later extract that information.

    Code:
    var array = [];
            
            for (var i = 0; i < 2; i++) {
                var s = prompt("Enter the text you want to pass to the eval function: ", "");
                array[i] = eval(s);
            }
            
            console.log(array.toString());
    Last edited by tech_soul8; 01-26-2014 at 09:15 AM.

  3. #3
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,517
    In programming terms, the use of eval in this way constitutes abuse of the eval function.

    You say that you want to use read[i] but you don't define what 'i' is but you want to return 3 elements, one of which contains i

    Perhaps if you explain exactly what you want to do and the purpose of this need then perhaps it might lead to a block of code that is more appropriate.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  4. #4
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,443
    To add to '\\.\'s question,
    is it a requirement to use prompt to acquire the information?

    Solution WITHOUT prompt or eval usage...
    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8" />
    
    <title> HTML5 page </title>
    
    </head>
    <body>
    <div id="debug"></div>
    
    <script type="text/javascript">
    var inputList = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    document.getElementById('debug').innerHTML = inputList.join('<br>');
    
    var ijk = [];
    while (inputList.length > 0) {
      i = inputList.shift();
      j = inputList.shift();
      ijk.push(i*j);
    }
    document.getElementById('debug').innerHTML += '<p>ijk array:<br>'+ijk.join('<br>');
    </script>
    
    </body>
    </html>

  5. #5
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,443
    To add to '\\.\'s question,
    is it a requirement to use prompt to acquire the information?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles