Need to get a number using only one loop for and charCodeAt()
We can't use native methods and concatenations, parseInt, parseFloat, Number, +str, 1 * str, 1 / str, 0 + str, etcc
const text = "In this market, I lost 0,0000695 USDT, today is not my day";
function getAmount(str) {
const zero = "0".charCodeAt(0);
const nine = "9".charCodeAt(0);
const coma = ",".charCodeAt(0);
let num = 0,
factor = 1;
for (let i = str.length - 1; i >= 0; i--) {
const char = str.charCodeAt(i);
if (char >= zero && char <= nine) {
num += (char - 48) * factor;
factor *= 10;
}
}
return num;
};
console.log(getAmount(text));
Need result: 0,0000695
My current result is 695
Tell me how to correct the formula for writing zeros
When you hit a comma, you need to convert the current value to a fraction:
I just noticed that my answer is similar to a previously deleted answer. The issue with that answer is that they did not handle multiple commas in the string.