-2

I have a function that uses 3 variables and a try catch. I want to use the 3 variables as part of an array but if i do i cannot complete the try catch and function correctly

JS:

function calculateVolume() {

    length = document.getElementById("Length").value;
    width = document.getElementById("Width").value;
    height = document.getElementById("Height").value;
    try {
        if ((length > 1000) || (width > 1000) || (height > 1000))
            throw "err"
        else
            var volume = length * width * height;
        alert(volume);
    } catch (err) {
        alert("You have not entered all three dimensions correctly");
    }
}
arman1991
  • 1,166
  • 1
  • 18
  • 28
  • 2
    what error you getting? – Raja Sekar Mar 26 '15 at 13:13
  • 2
    Your code works fine. What exactly is the problem? – georg Mar 26 '15 at 13:14
  • As a note: while it is possible to throw what every you want as an error in js, you still should use a real Error object. You might want to read the [answer](http://stackoverflow.com/a/27501348/1960455) to the question [Throwing strings instead of Errors](http://stackoverflow.com/questions/11502052) – t.niese Mar 26 '15 at 13:15
  • 1
    @tymeJV possibly you don't know but in js [var_hoisting](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var#var_hoisting) – Grundy Mar 26 '15 at 13:15
  • 2
    Where does the array come into this - I'm not seeing it? Are length, width and height meant to be in an array? – Andy Mar 26 '15 at 13:16

1 Answers1

0

If I'm reading your question right, you want length, width, and height in an array and you want the condition to check their relevant values.

You could use some, as in:

arr.some(function (el) { return el > 1000; });

This example which would throw an error:

var a = 1000,
    b = 1000,
    c = 2000;

var arr = [a, b, c];

try {
    if (arr.some(function (el) { return el > 1000; })) {
        throw "err"
    } else {
        console.log('no error')
    }
} catch (err) {
    console.log("You have not entered all three dimensions correctly");
}

DEMO

Andy
  • 61,948
  • 13
  • 68
  • 95