构建计算器。
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 :(得分:2)
您可以创建一个属性设置为算术运算符的对象。请注意,.replace()
可能没有必要
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;

答案 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);