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.

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

Leave a Comment