Replacing some numbers in javascript

CalebH

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: Script

Now I get this question here: Tricky question. 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();
  }
}            
astarmist

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

replacing numbers in a string with regex in javascript

From Dev

Replacing some characters from string using javascript

From Dev

JavaScript - Replace some letters with numbers

From Dev

Convert a number to a duodecimal, replacing numbers to one of 12 letters in JavaScript

From Dev

Convert a number to a duodecimal, replacing numbers to one of 12 letters in JavaScript

From Dev

Why have some external javascript files ?numbers?

From Dev

Javascript multiplication is not working properly for some numbers

From Dev

Why have some external javascript files ?numbers?

From Dev

Replacing some characters in a NSString

From Dev

Python replacing numbers in a list

From Dev

Replacing selective numbers with NaNs

From Dev

Replacing numbers with zeros in golang

From Dev

replacing of numbers to zero in a sum

From Dev

replacing of numbers to zero in a sum

From Dev

Replacing text in vector with numbers?

From Dev

Replacing numbers with letters in string

From Dev

Replacing a word with numbers

From Dev

JavaScript Array.sort doesn't work on some arrays of numbers

From Dev

Javascript - extract some numbers in parentheses from text file

From Dev

Replacing some of a attribute value with XSLT

From Dev

Replacing some of a attribute value with XSLT

From Dev

Replacing numbers in the last column of data

From Dev

PHPExcel replacing last three numbers

From Dev

Replacing end numbers in a string python

From Dev

RegEx fo replacing numbers in a string

From Dev

PHPExcel replacing last three numbers

From Dev

Replacing a character between numbers in UNIX

From Dev

Replacing numbers to text using conditionals

From Dev

Python - replacing string/symbol with numbers

Related Related

HotTag

Archive