0

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.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Linny
  • 818
  • 1
  • 9
  • 22
  • Maybe I'm missing something…what is giving you the error `expected exception: string` is that a test? a linter? -- sorry I just clicked the link. – Mark May 20 '19 at 19:43
  • 1
    @MarkMeyer The CodeStepByStep website. When I submit the answer, it returns the error provided. I’ve tried refreshing/leaving the website, and resubmitting, but it still doesn’t work. – Linny May 20 '19 at 19:44
  • It says *"throw an exception with the message..."*, are you sure you should be throwing a string? – jonrsharpe May 20 '19 at 19:47
  • @jonrsharpe “throw new Error(...);” doesn’t work – Linny May 20 '19 at 19:48
  • Then I'd suggest following the instructions under **Need help?** – jonrsharpe May 20 '19 at 19:50
  • 1
    I think the site's just derped. I've tried throwing a lot at it and I have no clue. I even tried creating a custom object called StringException (it complained it was an object), and creating a custom class (complained about the `class` keyword), and returning and printing instead of throwing... – Carcigenicate May 20 '19 at 20:07
  • 1
    Similar error for other problems that require throwing an exception. I think they just messed up JS exception handling on their end. The same problems work fine in other languages wrt exceptions being thrown. – Carcigenicate May 20 '19 at 20:21
  • …and yes, [you should not throw strings](https://stackoverflow.com/q/11502052/1048572) anyway. – Bergi May 20 '19 at 21:24
  • @Bergi The numerous articles I read agree with your statement, as do I, but the exercise requires that **strings** must be thrown. – Linny May 20 '19 at 21:32
  • Yes, and this lets me question the usefulness of that website :-) As Carcigenicate observed, the exercise just doesn't seem to work. You might want to contact the author, or just carry on. I'll vote to close this question as off-topic or too broad since we cannot access the code of the test that is failing. – Bergi May 20 '19 at 21:37

0 Answers0