最近两次在牛客做网易的编程题,用JS写总是报错,有木有好心的大神帮我看看错哪了?
题一:奇怪的表达式求值
function fn(str){
var len = str.length;
var i = 0, output = 0;
var num = [], op = [];
str.replace(/(\d+)/g,function(a){
num.push(a);
});
str.replace(/[+|-|*]/g,function(a){
op.push(a);
})
var a, b, o;
while(op.length>0){
a = num.shift();
b = num.shift();
o = op.shift();
output = eval(a+o+b);
num.unshift(output);
}
return output;
}
function main(){
var str = '11+5*100';
console.log(fn(str));
}
main();
题二:忘记具体题目了,好像是集合,也是报内部错误,心好累T T
function fn(w,x,y,z){
if(!(w>=1&&x>=1&&w=1&&1<=z&&z<=100&&y<=z))
return 0;
var i = 0, j = 0, obj = {},tmp=0;
for(i=w;i<=x;i++){
for(j=y;j<=z;j++){
tmp = i/j;
obj[tmp]=1;
}
}
var output = 0;
for(var item in obj){
output++;
}
return output;
}
console.log(fn(1,10,1,1));
#网易##前端工程师#