Sovi.AI - AI Math Tutor

Scan to solve math questions

QUESTION IMAGE

jump to level 1 add a throw statement to the processnumbers function th…

Question

jump to level 1
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.
1 function processnumbers(toprocess) { // code will be tested with different values of toprocess
2 let result = 0;
3
4 for (let index = 0; index < toprocess.length; index++) {
5 / your solution goes here /
6
7 result += toprocessindex 1.3 index;
8 }
9
10 return result;
11 }
12

Explanation:

Step1: Check if element is not a number

if (isNaN(toProcess[index])) {

Step2: Throw error message

throw "An element in the list is not a number";

Answer:

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;
}