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]
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
javascript arrays string eval
marked as duplicate by epascarello
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.
add a comment |
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
javascript arrays string eval
marked as duplicate by epascarello
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
add a comment |
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
javascript arrays string eval
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
javascript arrays string eval
asked Nov 13 '18 at 18:18
Ian BrownIan Brown
61
61
marked as duplicate by epascarello
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
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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])
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
add a comment |
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>
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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])
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
add a comment |
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])
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
add a comment |
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])
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])
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
add a comment |
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
add a comment |
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>
add a comment |
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>
add a comment |
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>
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>
answered Nov 13 '18 at 18:38
Pablo DardePablo Darde
1,49811527
1,49811527
add a comment |
add a comment |
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