Home > Mobile >  Javascript For Loop using Arrays to find the Sum of Digits in a String Troubles
Javascript For Loop using Arrays to find the Sum of Digits in a String Troubles

Time:10-14

I've been learning javascript in school for a few months now and I'm trying to work a problem out on my own but I can't seem to figure out what the issue is. The assignment is to "Create a for loop that starts at zero and loops once for each character in the number using the .length property." So for example, if I were to put in 2514 it should return 12. Here is the piece I'm struggling with

function sumOfDigits(numberPassed) {

    if (!isNaturalNumber(numberPassed)) {
        return 'bad data';
    } else {
        numberPassed = parseInt(numberPassed)
        let digitSum = 0;
        for (let i = 0; i < numberPassed.length; i  ) {
            digitSum = numberPassed[i]   digitSum;

        }
        return digitSum;
    }
}

When I test it in the browser it keeps giving me 0. If anyone could point me in the right direction I would greatly appreciate it.

CodePudding user response:

function sumOfDigits(numberPassed) {

    if (!isNaturalNumber(numberPassed)) {
        return 'bad data';
    } else {
        numberPassed = parseInt(numberPassed)
        let digitSum = 0;
        for (let i = 0; i < numberPassed.toString().length; i  ) {
            digitSum = numberPassed[i]   digitSum;

        }
        return digitSum;
    }
}

It seems that your implementation is incredibly close to the solution that I found.

What the problem is, is that the value of parseInt(numberPassed) is no longer a string, which means its length cannot be read. So you have to one of many solutions. For example:

  1. Make numberPassed a string in the for loop by doing for (let i = 0; i < numberPassed.toString().length; i )

  2. Don't parse the numberPassed until it is ready to be used as a number. Or create a secondary variable that is the same just set to a string.

There are many other solutions to this problems, but for the most part it is moving around the parseInt() or the .toString() around with the numberPassed, so to make it easier in the future you can use variables similar to intNumberPassed = parseInt(numberPassed) and stringNumberPassed = numberPassed.toString() if you need to switch between the two often

If you would like more of an explanation or more solutions to the problem here they are.

So when you have a string you can measure the length easily, but when you convert it to a number, instead of a list of binary numbers like a word would be, it is a singular value, aka 12 would be 1100 but when you convert it to a string '12' is would be equivalent to 00110001 00110010 which are the unicode to binary values for '12', giving us a length of two, instead of a length of one.

CodePudding user response:

parseInt() would convert numberPassed into a number. Thus, you wont be able to perform .length method to it.

You'd have to convert it into a string through, numberPassed.toString() and then while you are performing your addition, convert each digit back to INT through parseInt(numberPassed[i]) to get your desired result.

The code would look something like this,

function sumOfDigits(numberPassed) {

    if (!isNaturalNumber(numberPassed)) {
        return 'bad data';
    } else {
        numberPassed = numberPassed.toString()
        let digitSum = 0;
        for (let i = 0; i < numberPassed.length; i  ) {
            digitSum = parseInt(numberPassed[i])   digitSum;

        }
        return digitSum;
    }
}

CodePudding user response:

Do not parse your number string into an int, if you want to loop over each digit like it was an array.

function isNaturalNumber(numberStr) {
  return /^\d $/.test(numberStr);
}

function sumOfDigits(numberPassed) {
  if (!isNaturalNumber(numberPassed)) {
    return 'bad data';
  } 
  let digitSum = 0;
  for (let i = 0; i < numberPassed.length; i  ) {
    digitSum  = parseInt(numberPassed[i], 10);
  }
  return digitSum;
}

console.log(sumOfDigits('2514')); // 12

If you want to parse it first, you will have to use some logarithmic math to extract each digit. String manipulation is less expensive in JavaScript, so this one is much-less performant.

function numDigits(n) {
  return Math.max(Math.floor(Math.log10(Math.abs(n))), 0)   1;
}

function getDigit(n, offset, fromLeft) {
  const magnitude = fromLeft ? numDigits(n) - offset : offset   1;
  return Math.floor((n / Math.pow(10, magnitude - 1)) % 10);
}

function isNaturalNumber(numberStr) {
  return /^\d $/.test(numberStr);
}

function sumOfDigits(numberPassed) {
  if (!isNaturalNumber(numberPassed)) {
    return 'bad data';
  }
  const natural = parseInt(numberPassed, 10);
  const digits = numDigits(natural);
  let digitSum = 0;
  for (let i = 0; i < digits; i  ) {
    digitSum  = getDigit(natural, i);
  }
  return digitSum;
}

console.log(sumOfDigits('2514')); // 12

Credit for getDigit calculation: https://stackoverflow.com/a/41712226/1762224

  • Related