Help with multiple if statements

Comments

5 comments

  • Stefan Ukena

    Hi Stephen,

    you are on the right track. You get an error, because you are trying to add two if-statements, while what you are trying to do is add two numbers, based on the conditions of two if-statements. To solve your problem you can use two variables, e.g. result1 and result2, that you assign values within each if statement, and then add those to variables at the end of your calculation:

    // First, let's set the values to zero (that way you don't need the "else" parts below):
    var result1 = 0;
    var result2 = 0;
    
    // Now set the values depending on your conditions:
    if (@16pp Sheetwork > 0) { 
       result1 = @Qty / 1000 * @16pp Sheetwork * 16.00 + 55;
    }
    
    if (@8pp Sheetwork > 0) { 
       result2 = @Qty / 1000 * @8pp Sheetwork * 18.00 + 50;
    }
    
    // Finally we will add those two values as the final result:
    result1 + result2;
    

    BTW: JavaScript provides some syntactic sugar that let's you write this in an even more compact way. It's simply a shorthand for the if/else:

    var result1 = (@16pp Sheetwork > 0) ? @Qty / 1000 * @16pp Sheetwork * 16.00 + 55 : 0;
    var result2 = (@8pp Sheetwork > 0) ? @Qty / 1000 * @8pp Sheetwork * 18.00 + 50 : 0;
    result1 + result2;
    

    Best,
    Stefan

    dieKollaborateure.com - Podio Training+Consulting+Developmentauch auf Deutsch

    0
    Comment actions Permalink
  • Rainer Grabowski (FVM)

    Hi Steve,

    for the reason why your IF-construction doesn't work see here http://www.w3schools.com/js/js_if_else.asp.
    But you need also the IF-condition: Sum only if 6 or more fields have a value.
    This script does it (it's only one possibility to calculate what you want). The text behind // is my comment which can be deleted). I use the short syntax: (if) value > 0 ? than this : else that

    var $qty = @Qty / 1000;
    var $16pp = @16pp Sheetwork ;
    var $8pp = @8pp Sheetwork;
    // add the other 7 fields
    
    var $16ppCheck = $16pp  > 0 ? 1 : 0;
    var $8ppCheck = $8pp  > 0 ? 1 : 0;
    //add the other 7
    var $valueCheck = $16ppCheck + $8ppCheck // + the other 7; this checks, if there are minimum 6 fields > 0
    
    var $16ppCalc = $16pp  > 0 ?  $qty * $16pp * 16.00 + 55 : 0; 
    var $8ppCalc = $8pp  > 0 ?  $qty * $8pp * 18.00 + 50 : 0; 
    // add the other 7
    var $sumCalc = $16ppCalc + $8ppCalc //+ the other 7
    
    $valueCheck >= 6 ? $sumCalc : 0 // this gets the sum if more min. 6 fields have a value
    

    Rainer

    0
    Comment actions Permalink
  • Rainer Grabowski (FVM)

    oh, a bit late :)

    0
    Comment actions Permalink
  • Stephen Leverington

    Thanks everyone for your help.
    Thanks to Stefan for your compact code.
    All works well.

    Regards

    Steve

    0
    Comment actions Permalink
  • Stefan Ukena

    Good morning everyone. :) Podio really should switch from Zendesk to Podio-Chat for their help forum. Then we would be able to tell if somebody else is already typing an answer, while they are typing it. ;)

    0
    Comment actions Permalink

Please sign in to leave a comment.

Powered by Zendesk