var txt = '54839257+32648-34324';
var x;
for (x of txt) {
if (0||1||2||3||4||5||6||7||8||9) {
var con = Number(x);
} else { x.toString();}
document.write(con);
}
In the code above i want to convert the digits inside the string to number but not plus and minus signs. I want to have them together for the result. like this: 54839257+32648-34324.
But my code gives this: 54839257NaN32648NaN34324.
if (0|1|2|3|4|5|6|7|8|9)will always betrue. You're not actually checking whether it's any of those numbers but performing bitwise arithmetic.if (x == '0' || x == '1' || ...)x.toString()doesn't do anything, you need to do something with the result. Also,xis already a string, why do you think you need to convert it to a string.