0

I'm trying to add the 2 variable that are in 2 different function. But it's giving an undefined result. the n_standard and n_quad is initialized

var n_standard = 0;
var n_quad = 0;
var totalQuad = quadRoom();
var totalStandard = standardRoom();
var total = totalStandard + totalQuad;

function standardRoom() {

  n_standard = n_standard + Number($("#selectBoxStandard").val());

  var totalStandard = {{$availableRooms[0]['nightRate'] * $n_nights }} * n_standard;


      $("#totals ul").html('<li>Total:'+ total +'</li>');
      return totalStandard;

}

  function quadRoom()
{
    n_quad = n_quad + Number($("#selectBoxQuad").val());

   var totalQuad = {{$availableRooms[1]['nightRate'] * $n_nights}} * n_quad;

   return totalQuad;


}
0

1 Answer 1

1

You're declaring them twice. remove var in the functions. Declaring them in the top scope makes them available everywhere.

Also, the functions quadRoom and standardRoom aren't available to those variables as they're only defined once the line they're on is reached.

You can try declaring the functions as variables with arrow functions.
In general...

var x = (a, b) => {return a * b};

In your case..

var totalQuad;
 var quadRoom = () => {
  n_quad = n_quad + Number($("#selectBoxQuad").val());
  totalQuad = {{$availableRooms[1]['nightRate'] * $n_nights}} * n_quad;

return totalQuad;
}
Sign up to request clarification or add additional context in comments.

4 Comments

We're talking about totalQuad and totalStandard, right?
The total is the one that is undefined. Sorry for the confusion. The totalQuad and totalStandard should be added.
Sorry I don't understand. Can you give me a sample?
I'ts still not working, do I do that to both quadRoom and standardRoom? Even if I do both, the total is NaN

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.