2016-05-28 3 views
0

電卓を構築する。デリートされたURIの16進コードにreplace()を使用して除算演算子

var process = "6÷6"; // need to replace division sign with one that javascript can evaluate with 
process = encodeURI(process); 
process.replace(/%C3%B7/gi,'/'); // replacement step that doesn't work - %C3%B7 is what shows up as the hex divison sign in chrome debugger, not sure why 
process = decodeURI(process); 
result = eval(process); 
+0

作品。 Chrome 50.0とSafari 9.1.1 –

答えて

1

必要はないかもしれません。 replace関数の戻り値を変数に代入する必要があります。最も簡単な方法は、自分自身にそれを割り当てることです:

process = process.replace(/%C3%B7/gi,'/'); 

だから、全体のスクリプトコードは次のようになります。私のために

var process = "6÷6"; // need to replace division sign with one that javascript can evaluate with 
process = encodeURI(process); 
process = process.replace(/%C3%B7/gi,'/'); // replacement step now works 
process = decodeURI(process); 
result = eval(process); 
2

プロパティを算術演算子に設定してオブジェクトを作成できます。注、.replace()はあなたのコードの3行目は間違っている

var map = {"÷":"/"}; 
 
var operatorType = "÷"; 
 
var process = "6" + map[operatorType] + "6"; // need to replace division sign with one that javascript can evaluate with 
 
process = encodeURI(process); 
 
process.replace(/%C3%B7/gi,'/'); // replacement step that doesn't work - %C3%B7 is what shows up as the hex divison sign in chrome debugger, not sure why 
 
process = decodeURI(process); 
 
result = eval(process); 
 
document.body.innerHTML = result;

関連する問題