How can I count the number and calculate it? (i using isNaN())

My code is below. I’m using isNaN() but the problem is it’s still valid

function numberSearch(str) {
  let sum = 0
  let strCount = 0
  
  if (str === "") {
    return 0
  };
  
  for (let i = 0; i < str.length; i++) {
    if (isNaN(Number(str[i]))) {
      strCount = strCount + 1 // if it's true, +1
    }
    
    sum = sum + Number(str[i]) // if it's a number 
  }
  
  return Math.round(sum / strCount);
}

//debugger;

let output = numberSearch('Hello6 ');
console.log(output); // --> 1

output = numberSearch('Hello6 9World 2,');
console.log(output); // --> 1

How can I count the number and calculate it?

When I using debugger sum is NaN .. I cant understand well.

31 thoughts on “How can I count the number and calculate it? (i using isNaN())”

  1. The issue is that sum = sum + Number(str[i]) is being executed even if isNaN(Number(str[i])) === true because it is outside of the if block. You need to wrap it inside of the else block so that it only executes if the previous condition is false.

    function numberSearch(str) {
      let sum = 0
      let strCount = 0
    
      if (str === "") {
        return 0
      };
    
      for (let i = 0; i < str.length; i++) {
        if (isNaN(Number(str[i]))) {
          strCount = strCount + 1 // if it's true, +1
        } else {
          sum = sum + Number(str[i]) // if it's a number 
        }
      }
    
      return Math.round(sum / strCount);
    }
    
    //debugger;
    
    let output = numberSearch('Hello6 ');
    console.log(output); // --> 1
    
    output = numberSearch('Hello6 9World 2,');
    console.log(output); // --> 1
    Reply
  2. Think of the following case

     for(let i = 0 ; i<str.length ; i++){
    
        //str[0] is 'H', so fall into the first if-case
        if(isNaN(Number(str[i]))){
          strCount = strCount + 1   // if it's true, +1
        }
    
        //the code keep running but 'H' shouldn't fall into the following case 
        sum = sum + Number(str[i]) // if it's a number     
    }
            
    return Math.round(sum/strCount);
    
    

    I suggest you add one more if-else for each case. If it doesn’t fall into the above case.

     for(let i = 0 ; i<str.length ; i++){
        if(isNaN(Number(str[i]))){
          strCount = strCount + 1   // if it's true, +1
        }else if(typeof str[i] === 'number'){
           sum = sum + Number(str[i]) // if it's a number 
        } 
      }
    
      return Math.round(sum/strCount);
    
    Reply
  3. Your main issue is that you add a number anyway. In the line sum = sum + Number(str[i]) is read for each and every character. So even the last char will be NaN and it would be added to the sum, making it NaN.

    I’m not quite sure what you’re trying to do here, it seems like you are trying to get the sum and divide it by the number of non number characters.

    You need to skip those in the sum. There are plenty of ways to do it, and some other problems with the code, but to keep the changes to your code to a minimum I say, just do this:

    function numberSearch(str) {
      let sum = 0;
      let strCount = 0;
      
      if (str === "") {
        return 0;
      };
      
      for (let i = 0; i < str.length; i++) {
        if (isNaN(Number(str[i]))) {
          strCount = strCount + 1; // if it's true, +1
        } else {
          sum = sum + Number(str[i]); // if it's a number 
        }
      }
      return Math.round(sum / strCount);
    }
    
    Reply
  4. I would probably do it slightly different than in the original code, and find out how many numbers there are, and work from there

    function numberSearch(str) {
      // get all the chars, filter the non-numbers
      const numbers = str.split('').filter( v => !isNaN(Number(v)));
      // map all the numbers to numbers and sum them up
      const sum = numbers.map( Number ).reduce( (agg, item) => agg + item, 0 );
      // divide the sum by the length of the string - the amount of numbers found
      // but always by at least 1 (so not to get a divide by 0 error when only all numbers are given)
      return Math.round( sum / Math.max(1, str.length - numbers.length) );
    }
    
    let output = numberSearch('Hello6 ');
    console.log(output); // --> 1
    
    output = numberSearch('Hello6 9World 2,');
    console.log(output); // --> 2
    
    output = numberSearch('111111111');
    console.log(output); // --> 9

    For the original code however, you should just add a continue in your if branch and your code will work (as long as not all characters are numbers, cause then you would divide by 0 and you didn’t specify how to handle such a case)

    for (let i = 0; i < str.length; i++) {
      if (isNaN(Number(str[i]))) {
        strCount = strCount + 1; // if it's true, +1
        continue;
      }
      
      sum = sum + Number(str[i]); // if it's a number 
    }
    

    Just remember to verify to not divde by 0 if all are numbers 🙂

    Reply

Leave a Comment