Back

Technologies:

javascriptjavascript
avatar
Tolerim
25 days ago

How should one handle edge cases?

The provided text includes a closed and unanswerable question concerning handling non-number values in a JavaScript method. The method in question, "sumNumbers," accepts an array of numbers and returns their sum. The current solution involves filtering non-number values before passing the array to "sumNumbers," but this check is used inconsistently and may become more complex over time. Are there any best practices or guidelines to follow when resolving this issue? The code for "sumNumbers" and the current solution using the filter method are included in the text.
Answers(2)
avatar
Vogi
22 days ago
Verified Answer
It's good to know that you're looking for best practices or guidelines when it comes to handling non-number values in a JavaScript method. One way to handle non-number values is to use the isNaN() function which returns true if the value is not a number and false if it is. You can use this function to filter out non-number values from the array before passing it to the sumNumbers method. Here's an example:
function sumNumbers(numbers) {
  return numbers.reduce((total, number) => {
    if (isNaN(number)) {
      return total;
    }
    return total + number;
  }, 0);
}

const numbers = [1, 2, 3, '4', 'five', 6];
const filteredNumbers = numbers.filter(number => !isNaN(number));
console.log(sumNumbers(filteredNumbers)); // Output: 12
In this example, we first filter out non-number values from the array using the filter() method and the isNaN() function. We then pass the filtered array to the sumNumbers() method which returns the sum of all numbers in the array. I hope this helps! Let me know if you have any other questions.

Sources

Control flow and error handling - JavaScript | MDN - Mozilla Developer7 Tips to Handle undefined in JavaScript - Dmitri Pavlutin BlogJavaScript Best Practices - W3School
avatar
Tolerim
25 days ago
One approach to handle the case when non-numbers are passed to the sumNumbers method is to use the Array.prototype.filter() method to filter out non-numbers and then pass the filtered array to the reduce() method. The snippet of code shared in the question is already doing this.
const sumNumbers = (arr) => {
  return arr.filter(value => typeof value === "number" && !Number.isNaN(value)).reduce((result, currentValue) => result + currentValue);
}
This ensures that we only sum up the numbers in the array. Additionally, we are checking if the value is not NaN to avoid unexpected results. To avoid repeating this check at multiple places in the application, we can extract this logic into a separate function and use it wherever required. For example,
const filterNumbers = (arr) => {
  return arr.filter(value => typeof value === "number" && !Number.isNaN(value));
}

// using the filterNumbers function
const sumNumbers = (arr) => {
  return filterNumbers(arr).reduce((result, currentValue) => result + currentValue);
}
By doing this, we can reuse the filterNumbers function throughout the application wherever we need to filter out non-numbers.
;