Ask Question
17 January, 06:48

Add a throw statement to the processNumbers function that throws the message "All elements in the list should be numbers." if one of the elements in numberList is not a number. Hint: The function isNaN () returns true if the parameter is not a number.

+5
Answers (1)
  1. 17 January, 07:20
    0
    function processNumbers (numList) { try{ for (let i=0; i < numList. length; i++) { if (isNaN (numList[i])) { throw "All elements in the list should be numbers."; } } } catch (err) { console. log (err); } } let myList = [4, 6, 1, "abc", 9]; processNumbers (myList);

    Explanation:

    The solution code is written in jа vascript as the isNaN () is an in-built function in jа vascript.

    Firstly, create a function processNumbers that takes one input number list, numList (Line 1).

    To handle the possible Not a Number (NaN) error, create a try and catch block. In the try block, create a for-loop to traverse through the numList array and use isNaN function to check if the current value is a number (Line 3 - 4). If it is not a number, isNaN will return true and the program will run the throw statement (Line 5). The throw message will be passed to the catch block and display it on console (Line 10 - 12).

    We can test the function by using the sample list (Line 15 - 16) and we shall see the thrown error message is displayed.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Add a throw statement to the processNumbers function that throws the message "All elements in the list should be numbers." if one of the ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers