First and foremost, I’ve been learning JS for about 2-3 hours. I’ve lost track. It’s 5 AM. I’m at a hackathon and trying to write my java application as a webapp. I have 7 hours. Any help would be greatly appreciated. I also know that eval is a terrible idea. However, it’s the easiest thing for my purpose right now. That being said:
edit for clarity So essentially, I have a little hangman-style learn coding game: I have a library of very short, non-buggy code snippets. The program introduces bugs (omissions mostly) to the code, and displays the buggy code. The player then attempts to either correct the code or, if they choose, to write a different snippet that mirrors the intended result of the displayed, buggy code. They input their answer in a textarea, and the program then compares the results of the execution of both code snippets (buggy snippet and user supplied snippet). The user wins if the outputs match.
Would there happen to be a way to compare result to another variable containing another eval?
as in:
edit for clarity The logic behind the following code snippet is:
There is a variable, result, which I am trying to use to reference the result of the call to eval() a value (potentially buggy code snippet) in an input area (game.input). The array goodCode contains the good code; i.e. non-buggy code. I want to call eval() on both snippets of code (the one from goodCode and the one from game.input), and compare the output of their executions. if their executions are the same, the program will output the successful execution, and a message that says “winner”.
goodCode = new Array("document.write('<b>Hello World</b>');")
var goodResult = eval(goodCode[0])
function executeJS()
{
var game = document.forms['game'];
var result = eval(game.input.value);
var answer = eval(goodCode[0]);
if (result === goodResult)
{
game.execute.value = result;
document.write("winner!")
}
}
<head>
<body>
<form name="game">
<textarea type=text name="input" rows="10" cols="30" value=""></textarea><br>
<input type="button" value="guess" onclick="executeJS();"
</form>
</body>
</head>
I’m sure my terminology is really off in my explanation. Sorry about that.
Any help would be appreciated.
If both code snippets returned a value, you could compare them. But
document.write()etc. do not return anything useful the only thing you could do is comparing the code itself without executing it.