Javascript Function Creation Question 7

Introduction

What is the output of the following code?


function sumNumbers(numArray) {
  let result = 0;

  // sum numbers in array
  // unless array is empty, or non-number reached
  if (numArray.length > 0) {
    for (let i = 0; i < numArray.length; i++) {
      if (typeof numArray[i] == "number") {
        result += numArray[i];// www .j a  v a  2s. c o  m
      } else {
        result = NaN;
        break;
      }
    }
  } else {
    result = NaN;
  }
  return result;
}

let ary = new Array(1, 15, "three", 5, 5);
let res = sumNumbers(ary); 
console.log(res);


NaN



PreviousNext

Related