I'm having an issue with throwing an error type string
. I've spent twenty minutes looking at all the other questions, implementing them into my own code. Most of them are related to C# and Java, and only closest one I've found was in TypeScript, which didn't work.
The goal is to throw a string
when the array is null or the length is zero. I've tried the tens of ways they describe it on this website, to no avail. The link to this question is here.
function minAndMax(arr) {
if(arr === null || arr.length == 0) { throw "array must contain at least one value"; }
let min = arr[0];
let max = arr[0];
for(let i = 0; i < arr.length; i++) {
if(arr[i] < min) { min = arr[i]; }
if(arr[i] > max) { max = arr[i]; }
}
console.log("Minimum Value = " + min);
console.log("Maximum Value = " + max);
}
I am given the following error:
expected exception: string
your exception: String: array must contain at least one value
I've researched String vs string literal
and any other case that it related to this, but I've found no help. There are examples that work and they use the same syntax as I do, but mine doesn't work.