QUESTION IMAGE
Question
add a throw statement to the processnumbers function that throws the message \an element in the list is not a number\ if one of the elements in toprocess is not a number.
note: the function isnan() returns true if the parameter is not a number.
function processnumbers(toprocess) { // code will be tested with different values of toprocess
let result = 0;
for (let index = 0; index < toprocess.length; index++) {
throw \an element in the list is not a number\;
result += toprocessindex 1.3 index;
}
return result;
}
testing if an exception is thrown with toprocess = 1, 2, 5, 7
yours and expected differ. see highlights below.
yours: yes
expected: no
further testing is suppressed until the above passes.
exploring further:
- try...catch (mdn)
- error object (mdn)
Step1: Check if element is not a number
Inside the for loop, add an if statement to check if isNaN(toProcess[index]) is true.
if (isNaN(toProcess[index])) {
Step2: Throw error message
If the condition is met, throw the specified error message.
throw "An element in the list is not a number";Snap & solve any problem in the app
Get step-by-step solutions on Sovi AI
Photo-based solutions with guided steps
Explore more problems and detailed explanations
function processNumbers(toProcess) {
let result = 0;
for (let index = 0; index < toProcess.length; index++) {
if (isNaN(toProcess[index])) {
throw "An element in the list is not a number";
}
result += toProcess[index] * 1.3 * index;
}
return result;
}