Setting a variable in Google Scripts based on another variable

Tags: ,



I am new to Javascript and slowing using Apps Scripts by google to practise, firstly, how do assign the value of a global variable to another one depending on if statements. Every test of the code below gives me the last value.

var oneweek = 8640, onefort = 4696, onemonth = 2628, threeweek = 7728, threefort = 4290, threemont = 2401;

var totalprice = 0;

switch (true) { 
    case (contract_len == "1 Year" && time_len == "Weekly"):
        if (day_options == "Full Day") {
          var totalprice = oneweek;
    }
        else {
          var totalprice = oneweek / 2;
        break;
    }
    
    case (contract_len == "1 Year" && time_len == "Fortnightly"):
        if (day_options == "Full Day") {
          var totalprice = onefort;
    }
        else {
          var totalprice = onefort / 2;
        break;
    }
  
    case (contract_len == "1 Year" && time_len == "Monthly"):
        if (day_options == "Full Day") {
          var totalprice = onemonth;
    }
        else {
          var totalprice = onemonth / 2;
        break;
    }
    case (contract_len == "3 Years" && time_len == "Weekly"):
        if (day_options == "Full Day") {
          var totalprice = threeweek;
    }
        else {
          var totalprice = threeweek / 2;
        break;
    }
 
    case (contract_len == "3 Years" && time_len == "Fortnightly"):
        if (day_options == "Full Day") {
          var totalprice = threefort;
    }
        else {
          var totalprice = threefort / 2;
        break;
    }
  
    case (contract_len == "3 Years" && time_len == "Monthly"):
    if (day_options == "Full Day") {
        var totalprice = threemont;
    }
    else {
        var totalprice = threemont / 2;
        break;
    }
  }

Is there a way to simplify this? I am using the on Submit form trigger.

Answer

Issue:

The issue in your code is that you are not breaking when the first if condition holds true.

For example:

 switch (true) { 
    case (contract_len == "1 Year" && time_len == "Weekly"):
        if (day_options == "Full Day") {
          var totalprice = oneweek;
    }
        else {
          var totalprice = oneweek / 2;
        break;
    }
    
    case (contract_len == "1 Year" && time_len == "Fortnightly"):
        if (day_options == "Full Day") {
          var totalprice = onefort;
    }
        else {
          var totalprice = onefort / 2;
        break;
    }

If the first case statement is true and the first if statement as well, the value of totalprice will be equal to oneweek. The issue is that you forget to break. Therefore the next case block will be automatically true, and therefore totalprice will become onefort (assuming again that day_options="Full Day").

That explains why you are getting the last value everytime.

The quick fix in your code is to put the break statement after the if blocks for every case statement.

Solution:

function myFunction() {
  
  var oneweek = 8640, onefort = 4696, onemonth = 2628, threeweek = 7728, threefort = 4290, threemont = 2401;
  contract_len = "1 Year"
  time_len = "Weekly"
  day_options = "Full Day"
  
  var totalprice = 0;

switch (true) { 
    case (contract_len == "1 Year" && time_len == "Weekly"):
        if (day_options == "Full Day") {
          var totalprice = oneweek;       
    }
        else {
          
          var totalprice = oneweek / 2;
    }
    break;
    
    case (contract_len == "1 Year" && time_len == "Fortnightly"):
        if (day_options == "Full Day") {
          var totalprice = onefort;
    }
        else {
          var totalprice = onefort / 2;        
    }
    break;
  
    case (contract_len == "1 Year" && time_len == "Monthly"):
        if (day_options == "Full Day") {
          var totalprice = onemonth;
    }
        else {
          var totalprice = onemonth / 2;
    }
        break;
    case (contract_len == "3 Years" && time_len == "Weekly"):
        if (day_options == "Full Day") {
          var totalprice = threeweek;
    }
        else {
          var totalprice = threeweek / 2;
    }
       break;
 
    case (contract_len == "3 Years" && time_len == "Fortnightly"):
        if (day_options == "Full Day") {
          var totalprice = threefort;
    }
        else {
          var totalprice = threefort / 2;
        
    }
    break;
  
    case (contract_len == "3 Years" && time_len == "Monthly"):
    if (day_options == "Full Day") {
        var totalprice = threemont;
    }
    else {
        var totalprice = threemont / 2;
        
    }
    break;
  }
  
}

If you are looking to improve your code, this is not the right platform to request this. Instead I would advice you to post a question in the code review.

However, if you are looking to shorten your code, you can use ternary operators.



Source: stackoverflow