skip to Main Content

I am trying to write a script that will add 5% to a subtotal and no matter what I try it won’t work. Here is an example. I know Excel formulas, but don’t write Javascript, please help.

// (Function added as source of dummy data)
function getField(key) {
    return ({
        EXTENSION1: {value:'110.0'},
        EXTENSION2: {value:'95.0'},
        EXTENSION3: {value:'85.0'},
        EXTENSION4: {value:'65.0'},
        EXTENSION5: {},
        'GE%': {value: '5'},
    })[key]
}

var extension1 = this.getField("EXTENSION1").value;
var extension2 = this.getField("EXTENSION2").value;
var extension3 = this.getField("EXTENSION3").value;
var extension4 = this.getField("EXTENSION4").value;
var extension5 = this.getField("EXTENSION5").value;

var sum = parseFloat(extension1) + parseFloat(extension2) + parseFloat(extension3) + parseFloat(extension4) + parseFloat(extension5);

var percentage = parseFloat(this.getField("GE%").value);

var result = sum * (percentage / 100);

console.log(result);

I’ve entered this code and it’s not producing a value

enter image description here

2

Answers


  1. I have created this snippet to implement your solution and it works for me:

    let extension1 = 0;
    let extension2 = 110;
    let extension3 = 95;
    let extension4 = 85;
    let extension5 = 65;
    let percentage = 5.5;
    
    var sum = parseFloat(extension1) + parseFloat(extension2) + parseFloat(extension3) + parseFloat(extension4) + parseFloat(extension5);
    
    var result = sum * ((100 + percentage) / 100);
    
    console.log(result);

    Key ideas:

    • you should make sure that your extension values are proper values. If you have non-numeric values to any of them, the result will be NaN
    • you multiply sum with percentage / 100, but you want to add that to sum, which means that you need 100 + percentage instead of just `percentage
    • make sure you use the result
    Login or Signup to reply.
  2. Your code doesn’t properly account for when a field hasn’t been filled out.

    A better solution

    You can use detect if a value is NaN and instead set it to 0. The parts I edited are between the markers.

    // (Function added as source of dummy data)
    function getField(key) {
        return ({
            EXTENSION1: {value:'110.0'},
            EXTENSION2: {value:'95.0'},
            EXTENSION3: {value:'85.0'},
            EXTENSION4: {value:'65.0'},
            EXTENSION5: {},
            'GE%': {value: '5'},
        })[key]
    }
    
    var extension1 = this.getField("EXTENSION1").value;
    var extension2 = this.getField("EXTENSION2").value;
    var extension3 = this.getField("EXTENSION3").value;
    var extension4 = this.getField("EXTENSION4").value;
    var extension5 = this.getField("EXTENSION5").value;
    
    // ------------------------
    const inputArray = [
        extension1,
        extension2,
        extension3,
        extension4,
        extension5,
    ];
    const sum = inputArray.reduce(
        (accumulator, currentValue) => {
            let val = parseFloat(currentValue);
            if (isNaN(val)) {
                val = 0;
            }
            return accumulator + val;
        },
        0
    );
    // ------------------------
    
    var percentage = parseFloat(this.getField("GE%").value);
    
    var result = sum * (percentage / 100);
    
    console.log(result);

    An okay solution

    Your updated summation code could simply fail-over to 0 if the string is empty.

    var sum = parseFloat(extension1 || 0) + parseFloat(extension2 || 0) + parseFloat(extension3 || 0) + parseFloat(extension4 || 0) + parseFloat(extension5 || 0);
    

    Why?

    Passing an empty string to parseInt results in NaN:

    parseFloat('')
    > NaN
    
    Likewise, `NaN` plus anything is still `NaN`:
    ```js
    parseFloat('') + 1
    > NaN
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search