In javascript, is using eval() to match the text chosen in a dropdown to an array an alright use, or am i missing a better way to do this? [duplicate]










1
















This question already has an answer here:



  • Javascript, refer to a variable using a string containing its name?

    7 answers



In my program, I have the user fill in various fields and choose a pokemon, and it will calculate its stats. I have the arrays under the pokemon names, and a drop down to choose which pokemon's stats to calculate. I then use eval() to change the string in the dropdown into the array's name in order to do the calculations. The full program can be found with this link



https://studio.code.org/projects/applab/8N1yw5e0CcjTik0rsmp2mgW0TSoU7DSge8k8j5mlhw0



but a simplified version is



var venusaur = [20, 10, 5, 10, 20]
var blastoise = [50, 50, 50, 10, 5]
var charizard = [100, 10, 5, 10, 100]
var mon = getText("dropdown");
findStats(eval(mon));


before I added the eval, it was much clunkier code, and would only get worse as I added more pokemon, a simpler version of which is



var venusaur = [20, 10, 5, 10, 20];
var blastoise = [50, 50, 50, 10, 5];
var charizard = [100, 10, 5, 10, 100];
if (getText(dropdown)=="venusaur")
findStats(venusaur);
else if (getText(dropdown)=="blastoise")
findStats(blastoise);
else if getText(dropdown)=="charizard")
findStats(charizard);



I was just wondering if in this case, eval() was usable, as I've heard almost exclusively bad things about it, and I figured if there's a better way to accomplish this task, maybe I could learn it. Thanks in advance










share|improve this question













marked as duplicate by epascarello javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 13 '18 at 18:28


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















  • eval is almost never the solution....

    – epascarello
    Nov 13 '18 at 18:23











  • that's why i asked here, to find if this is an exception or if there's something i could do better

    – Ian Brown
    Nov 13 '18 at 18:24











  • Use an object with pokemon names as the keys instead.

    – Andy
    Nov 13 '18 at 18:25















1
















This question already has an answer here:



  • Javascript, refer to a variable using a string containing its name?

    7 answers



In my program, I have the user fill in various fields and choose a pokemon, and it will calculate its stats. I have the arrays under the pokemon names, and a drop down to choose which pokemon's stats to calculate. I then use eval() to change the string in the dropdown into the array's name in order to do the calculations. The full program can be found with this link



https://studio.code.org/projects/applab/8N1yw5e0CcjTik0rsmp2mgW0TSoU7DSge8k8j5mlhw0



but a simplified version is



var venusaur = [20, 10, 5, 10, 20]
var blastoise = [50, 50, 50, 10, 5]
var charizard = [100, 10, 5, 10, 100]
var mon = getText("dropdown");
findStats(eval(mon));


before I added the eval, it was much clunkier code, and would only get worse as I added more pokemon, a simpler version of which is



var venusaur = [20, 10, 5, 10, 20];
var blastoise = [50, 50, 50, 10, 5];
var charizard = [100, 10, 5, 10, 100];
if (getText(dropdown)=="venusaur")
findStats(venusaur);
else if (getText(dropdown)=="blastoise")
findStats(blastoise);
else if getText(dropdown)=="charizard")
findStats(charizard);



I was just wondering if in this case, eval() was usable, as I've heard almost exclusively bad things about it, and I figured if there's a better way to accomplish this task, maybe I could learn it. Thanks in advance










share|improve this question













marked as duplicate by epascarello javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 13 '18 at 18:28


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















  • eval is almost never the solution....

    – epascarello
    Nov 13 '18 at 18:23











  • that's why i asked here, to find if this is an exception or if there's something i could do better

    – Ian Brown
    Nov 13 '18 at 18:24











  • Use an object with pokemon names as the keys instead.

    – Andy
    Nov 13 '18 at 18:25













1












1








1









This question already has an answer here:



  • Javascript, refer to a variable using a string containing its name?

    7 answers



In my program, I have the user fill in various fields and choose a pokemon, and it will calculate its stats. I have the arrays under the pokemon names, and a drop down to choose which pokemon's stats to calculate. I then use eval() to change the string in the dropdown into the array's name in order to do the calculations. The full program can be found with this link



https://studio.code.org/projects/applab/8N1yw5e0CcjTik0rsmp2mgW0TSoU7DSge8k8j5mlhw0



but a simplified version is



var venusaur = [20, 10, 5, 10, 20]
var blastoise = [50, 50, 50, 10, 5]
var charizard = [100, 10, 5, 10, 100]
var mon = getText("dropdown");
findStats(eval(mon));


before I added the eval, it was much clunkier code, and would only get worse as I added more pokemon, a simpler version of which is



var venusaur = [20, 10, 5, 10, 20];
var blastoise = [50, 50, 50, 10, 5];
var charizard = [100, 10, 5, 10, 100];
if (getText(dropdown)=="venusaur")
findStats(venusaur);
else if (getText(dropdown)=="blastoise")
findStats(blastoise);
else if getText(dropdown)=="charizard")
findStats(charizard);



I was just wondering if in this case, eval() was usable, as I've heard almost exclusively bad things about it, and I figured if there's a better way to accomplish this task, maybe I could learn it. Thanks in advance










share|improve this question















This question already has an answer here:



  • Javascript, refer to a variable using a string containing its name?

    7 answers



In my program, I have the user fill in various fields and choose a pokemon, and it will calculate its stats. I have the arrays under the pokemon names, and a drop down to choose which pokemon's stats to calculate. I then use eval() to change the string in the dropdown into the array's name in order to do the calculations. The full program can be found with this link



https://studio.code.org/projects/applab/8N1yw5e0CcjTik0rsmp2mgW0TSoU7DSge8k8j5mlhw0



but a simplified version is



var venusaur = [20, 10, 5, 10, 20]
var blastoise = [50, 50, 50, 10, 5]
var charizard = [100, 10, 5, 10, 100]
var mon = getText("dropdown");
findStats(eval(mon));


before I added the eval, it was much clunkier code, and would only get worse as I added more pokemon, a simpler version of which is



var venusaur = [20, 10, 5, 10, 20];
var blastoise = [50, 50, 50, 10, 5];
var charizard = [100, 10, 5, 10, 100];
if (getText(dropdown)=="venusaur")
findStats(venusaur);
else if (getText(dropdown)=="blastoise")
findStats(blastoise);
else if getText(dropdown)=="charizard")
findStats(charizard);



I was just wondering if in this case, eval() was usable, as I've heard almost exclusively bad things about it, and I figured if there's a better way to accomplish this task, maybe I could learn it. Thanks in advance





This question already has an answer here:



  • Javascript, refer to a variable using a string containing its name?

    7 answers







javascript arrays string eval






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 18:18









Ian BrownIan Brown

61




61




marked as duplicate by epascarello javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 13 '18 at 18:28


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by epascarello javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 13 '18 at 18:28


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • eval is almost never the solution....

    – epascarello
    Nov 13 '18 at 18:23











  • that's why i asked here, to find if this is an exception or if there's something i could do better

    – Ian Brown
    Nov 13 '18 at 18:24











  • Use an object with pokemon names as the keys instead.

    – Andy
    Nov 13 '18 at 18:25

















  • eval is almost never the solution....

    – epascarello
    Nov 13 '18 at 18:23











  • that's why i asked here, to find if this is an exception or if there's something i could do better

    – Ian Brown
    Nov 13 '18 at 18:24











  • Use an object with pokemon names as the keys instead.

    – Andy
    Nov 13 '18 at 18:25
















eval is almost never the solution....

– epascarello
Nov 13 '18 at 18:23





eval is almost never the solution....

– epascarello
Nov 13 '18 at 18:23













that's why i asked here, to find if this is an exception or if there's something i could do better

– Ian Brown
Nov 13 '18 at 18:24





that's why i asked here, to find if this is an exception or if there's something i could do better

– Ian Brown
Nov 13 '18 at 18:24













Use an object with pokemon names as the keys instead.

– Andy
Nov 13 '18 at 18:25





Use an object with pokemon names as the keys instead.

– Andy
Nov 13 '18 at 18:25












2 Answers
2






active

oldest

votes


















3














Use an object and reference the object instead of using a bunch of global variables.



var pokes = 
venusaur: [20, 10, 5, 10, 20],
blastoise: [50, 50, 50, 10, 5],
charizard: [100, 10, 5, 10, 100]

var mon = getText("dropdown");
console.log(pokes[mon])





share|improve this answer























  • thanks, that helped a ton, for some reason my programming class never mentioned objects, so thanks for adding another tool to my repertoire

    – Ian Brown
    Nov 13 '18 at 18:37


















0














I think you're trying to do something like that...






const stats = 
venusaur: [20, 10, 5, 10, 20],
blastoise: [50, 50, 50, 10, 5],
charizard: [100, 10, 5, 10, 100],
;

const sel = document.querySelector('select');

const getStats = (e) => 'not found';


sel.addEventListener('change', getStats);

<select>
<option value="null">Select...</option>
<option value="venusaur">Venusaur</option>
<option value="blastoise">Blastoise</option>
<option value="charizard">Charizard</option>
</select>

<div id="result"></div>








share|improve this answer





























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    Use an object and reference the object instead of using a bunch of global variables.



    var pokes = 
    venusaur: [20, 10, 5, 10, 20],
    blastoise: [50, 50, 50, 10, 5],
    charizard: [100, 10, 5, 10, 100]

    var mon = getText("dropdown");
    console.log(pokes[mon])





    share|improve this answer























    • thanks, that helped a ton, for some reason my programming class never mentioned objects, so thanks for adding another tool to my repertoire

      – Ian Brown
      Nov 13 '18 at 18:37















    3














    Use an object and reference the object instead of using a bunch of global variables.



    var pokes = 
    venusaur: [20, 10, 5, 10, 20],
    blastoise: [50, 50, 50, 10, 5],
    charizard: [100, 10, 5, 10, 100]

    var mon = getText("dropdown");
    console.log(pokes[mon])





    share|improve this answer























    • thanks, that helped a ton, for some reason my programming class never mentioned objects, so thanks for adding another tool to my repertoire

      – Ian Brown
      Nov 13 '18 at 18:37













    3












    3








    3







    Use an object and reference the object instead of using a bunch of global variables.



    var pokes = 
    venusaur: [20, 10, 5, 10, 20],
    blastoise: [50, 50, 50, 10, 5],
    charizard: [100, 10, 5, 10, 100]

    var mon = getText("dropdown");
    console.log(pokes[mon])





    share|improve this answer













    Use an object and reference the object instead of using a bunch of global variables.



    var pokes = 
    venusaur: [20, 10, 5, 10, 20],
    blastoise: [50, 50, 50, 10, 5],
    charizard: [100, 10, 5, 10, 100]

    var mon = getText("dropdown");
    console.log(pokes[mon])






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 13 '18 at 18:25









    epascarelloepascarello

    152k13133183




    152k13133183












    • thanks, that helped a ton, for some reason my programming class never mentioned objects, so thanks for adding another tool to my repertoire

      – Ian Brown
      Nov 13 '18 at 18:37

















    • thanks, that helped a ton, for some reason my programming class never mentioned objects, so thanks for adding another tool to my repertoire

      – Ian Brown
      Nov 13 '18 at 18:37
















    thanks, that helped a ton, for some reason my programming class never mentioned objects, so thanks for adding another tool to my repertoire

    – Ian Brown
    Nov 13 '18 at 18:37





    thanks, that helped a ton, for some reason my programming class never mentioned objects, so thanks for adding another tool to my repertoire

    – Ian Brown
    Nov 13 '18 at 18:37













    0














    I think you're trying to do something like that...






    const stats = 
    venusaur: [20, 10, 5, 10, 20],
    blastoise: [50, 50, 50, 10, 5],
    charizard: [100, 10, 5, 10, 100],
    ;

    const sel = document.querySelector('select');

    const getStats = (e) => 'not found';


    sel.addEventListener('change', getStats);

    <select>
    <option value="null">Select...</option>
    <option value="venusaur">Venusaur</option>
    <option value="blastoise">Blastoise</option>
    <option value="charizard">Charizard</option>
    </select>

    <div id="result"></div>








    share|improve this answer



























      0














      I think you're trying to do something like that...






      const stats = 
      venusaur: [20, 10, 5, 10, 20],
      blastoise: [50, 50, 50, 10, 5],
      charizard: [100, 10, 5, 10, 100],
      ;

      const sel = document.querySelector('select');

      const getStats = (e) => 'not found';


      sel.addEventListener('change', getStats);

      <select>
      <option value="null">Select...</option>
      <option value="venusaur">Venusaur</option>
      <option value="blastoise">Blastoise</option>
      <option value="charizard">Charizard</option>
      </select>

      <div id="result"></div>








      share|improve this answer

























        0












        0








        0







        I think you're trying to do something like that...






        const stats = 
        venusaur: [20, 10, 5, 10, 20],
        blastoise: [50, 50, 50, 10, 5],
        charizard: [100, 10, 5, 10, 100],
        ;

        const sel = document.querySelector('select');

        const getStats = (e) => 'not found';


        sel.addEventListener('change', getStats);

        <select>
        <option value="null">Select...</option>
        <option value="venusaur">Venusaur</option>
        <option value="blastoise">Blastoise</option>
        <option value="charizard">Charizard</option>
        </select>

        <div id="result"></div>








        share|improve this answer













        I think you're trying to do something like that...






        const stats = 
        venusaur: [20, 10, 5, 10, 20],
        blastoise: [50, 50, 50, 10, 5],
        charizard: [100, 10, 5, 10, 100],
        ;

        const sel = document.querySelector('select');

        const getStats = (e) => 'not found';


        sel.addEventListener('change', getStats);

        <select>
        <option value="null">Select...</option>
        <option value="venusaur">Venusaur</option>
        <option value="blastoise">Blastoise</option>
        <option value="charizard">Charizard</option>
        </select>

        <div id="result"></div>








        const stats = 
        venusaur: [20, 10, 5, 10, 20],
        blastoise: [50, 50, 50, 10, 5],
        charizard: [100, 10, 5, 10, 100],
        ;

        const sel = document.querySelector('select');

        const getStats = (e) => 'not found';


        sel.addEventListener('change', getStats);

        <select>
        <option value="null">Select...</option>
        <option value="venusaur">Venusaur</option>
        <option value="blastoise">Blastoise</option>
        <option value="charizard">Charizard</option>
        </select>

        <div id="result"></div>





        const stats = 
        venusaur: [20, 10, 5, 10, 20],
        blastoise: [50, 50, 50, 10, 5],
        charizard: [100, 10, 5, 10, 100],
        ;

        const sel = document.querySelector('select');

        const getStats = (e) => 'not found';


        sel.addEventListener('change', getStats);

        <select>
        <option value="null">Select...</option>
        <option value="venusaur">Venusaur</option>
        <option value="blastoise">Blastoise</option>
        <option value="charizard">Charizard</option>
        </select>

        <div id="result"></div>






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 18:38









        Pablo DardePablo Darde

        1,49811527




        1,49811527













            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3

            Museum of Modern and Contemporary Art of Trento and Rovereto