skip to Main Content

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 = "Hello team, I checked my wallet balance, there is 0,0000341 USDT, I can not buy anything";

const parseBalance = (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(parseBalance(text));

Need result: 0.0000341
My current result is 341

Tell me how to correct the formula for writing zeros

2

Answers


  1. I would process from left to right and modify the factor by 0.1 for every number you find. You will also need to deal with the float precision issues in Javascript, so before returning the final number call the precisionRound function. This function will round to the appropriate precision according to how many numbers were processed.

    const text = "Hello team, I checked my wallet balance, there is 0,0000341 USDT, I can not buy anything";
    
    const parseBalance = (str) => {
      const zero = "0".charCodeAt(0);
      const nine = "9".charCodeAt(0);
      const coma = ",".charCodeAt(0);
      let num = 0,
      factor = 1;
      precision = 1;
    
      for (let i = 0; i <= str.length -1; i++) {
        const char = str.charCodeAt(i);
        if (char >= zero && char <= nine) {
          num += (char - 48) * factor;
          factor *= 0.1;
          precision += 1
        }
      }
      return precisionRound(num, precision);
    };
    
      function precisionRound(number, precision) {
        var factor = Math.pow(10, precision);
        return Math.round(number * factor) / factor;
      }
    
    console.log(parseBalance(text));
    Login or Signup to reply.
  2. When you hit a comma, you need to convert the current value to a fraction:

    const parseBalance = (str) => {
        const zero = "0".charCodeAt(0);
        const nine = "9".charCodeAt(0);
        const comma = ",".charCodeAt(0);
        const space = " ".charCodeAt(0);
        let num = NaN, // Init to NaN as a sentinal value. Could also use null.
            factor = 1;
    
        for (let i = str.length - 1; i >= 0; i--) {
            const char = str.charCodeAt(i);
            // Break when finished in case there are commas before the number
            if (char == space && !isNaN(num)) break;
            else if (char >= zero && char <= nine) {
                if (isNaN(num)) num = 0; // Reset from NaN to Zero, first time only
                num += (char - 48) * factor;
                factor *= 10;
            }
            // On comma, convert num to a fraction and reset factor
            // Ex: num = 123 -> num = .123
            else if (char == comma && !isNaN(num)) {
                num /= factor;
                factor = 1;
            }
        }
        return num;
    }
    
    const text = "Hello team, I checked my wallet balance, there is 0,0000341 USDT, I can not buy anything";
    console.log(parseBalance(text));

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search