0

I want to get two values out from the function for a specific condition (on "As_cm" values) and execute some operation on those values. I could not do that.

function steelSection() {
    if (As_cm <= 29.2) {
       return [D  = 152.4, B = 152.2];
    } else if (As_cm <= 38.3) {
       return {D = 157.6, B = 152.9];
    } else {
      return [D = 1000, B = 2000];
    }
}
var d = D / 2;
var b = B / 2;
console.log(d);
document.getElementById("flangeWidth").innerHTML = d ;
console.log(b);
document.getElementById("depth").innerHTML = b ;

expected values for As_cm <= 29.2

d= 152.4 / 2

b= 152.2 / 2

The error message I get is

Uncaught TypeError: Cannot set property 'innerHTML' of null at steelcolumn.js:68

Will Jenkins
  • 9,507
  • 1
  • 27
  • 46

1 Answers1

1

You could return an object and then take the destructured properties as values.

function steelSection() {
    if (As_cm <= 29.2) return { d: 152.4, b: 152.2 };
    if (As_cm <= 38.3) return { d: 157.6, b: 152.9 };
    return { d: 1000, b: 2000 };
}

var { d, b } = steelSection();

document.getElementById("flangeWidth").innerHTML = d / 2;
document.getElementById("depth").innerHTML = b / 2;
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392