2017-08-16 102 views
0

好的,所以我有一些JavaScript,基本上它從一個網站獲得一個數學公式,將其更改爲eval(),然後通過eval進行讀取,然後當我按一個鍵將eval()的答案插入到輸入框中。我有一個問題是,(例如)y +? = 20。「y」從數字1-20變化。到目前爲止,代碼得到了這個問題的答案,但是以負面的形式。 (例如:14 +?= 20 = -14)。所以我需要幫助從我的答案中刪除所有「 - 」字符。需要替換字符但替換不起作用

代碼變量:

var inputBox = where the answer is (like -14 in my case)  
var question = the question. in my case 14 + ? = 20  

到目前爲止的代碼:

//Mathletics Level 4 with Blocks// 
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 whiteTextWithShadow question-size-v4")[0].innerText; 
    question = question.replace('= 20', '-20') 
    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('= 20', '-20') 
    question = question.replace(' =', ''); 
    question = question.replace('×', '*') 
    question = question.replace('=', ''); 
    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() 
    } 
} 

回答

0

試試這個:

myanswer= myanswer.replace(/-/g,''); 

它的答案reomve負

0

你可以刪除減去使用下面的abs樂趣ction

  a =-3 
 
      x=Math.abs(a); 
 
      console.log(x)