I have a javascript code that goes into the console of my browser. This code is designed to be used on a maths website that asks questions to you.
Now my script is supposed to be pasted into the browser console, and this script basically removes the =
from the "question", puts the "question" into eval()
and then inputs the answer into the inputbox.
Here is that code:
Now I get this question here: . Here eval()
thinks that it is 16+20
and the answer keeps outputing as 36
.
Is there any way to change this to 20-16
? I cannot use .replace
to change the +
into a -
, as that will change it for the other +
questions as well.
function showAnswer() {
var inputBox = document.getElementsByClassName("questions-input-adjustment questions-input-width-v3")[0];
var submitButton = document.getElementsByClassName('question-input-form')[0];
for (var i = 0; i < 100; i++) {
var question = document.getElementsByClassName("questions-text-alignment")[0].innerText;
question = question.replace(' =', '');
question = question.replace('×', '*')
question = question.replace('=', '');
var answer = eval(question);
inputBox.value = answer;
}
var awnser = addbits(equasion)
var inputBox = document.getElementsByClassName("questions-input-adjustment questions-input-width-v3")[0];
var submitButton = document.getElementsByClassName('question-input-form')[0];
for (var i = 0; i < 100; i++) {
var question = document.getElementsByClassName("questions-text-alignment")[0].innerText;
question = question.replace(' =', '');
question = question.replace('×', '*')
question = question.replace('=', '');
var answer = eval(question);
inputBox.value = answer;
}
var awnser = addbits(equasion)
document.getElementById('dashow').innerText = awnser;
document.getElementsByClassName("questions-input-adjustment questions-input-width-v3")["0"].value = awnser;
}
window.addEventListener("keydown", checkKeyPressed, false);
function checkKeyPressed(e) {
if (e.keyCode == "81") {
showAnswer();
}
}
Looking at your source code and example, the original question string before the replacement would be: "16+ =20".
Your current code only handles this case:
(1) "16+20= "
You can use a regex expression to handle the scenarios when the blank is not the sum of the two numbers:
(2) " +16=20"
(3) "16+ =20"
var question = "16+ =20";
var regex = /(\s\+)|(\+\s)/i
if (regex.test(question)){
question = str.replace(" =", "");
question = str.replace("=","");
question = str.replace("+","-");
answer = -eval(question);
}
Jsfiddle: https://jsfiddle.net/nf2fqyv5/8/
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments