# Dynamic variable + Formula

**URL:** <https://community.postman.com/t/dynamic-variable-formula/14647>\
**Category:** Help Hub\
**Tags:** variables\
**Created:** [July 31, 2020, 3:05pm UTC](https://community.postman.com/t/dynamic-variable-formula/14647 "2020-07-31T15:05:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![zeballosfederico1](https://avatars.discourse-cdn.com/v4/letter/z/90db22/32.png) [@zeballosfederico1](https://community.postman.com/u/zeballosfederico1)\
**Post date:** [July 31, 2020, 3:05pm UTC](https://community.postman.com/t/dynamic-variable-formula/14647/1 "2020-07-31T15:05:31Z")

</div>

Hello everyone in the community.  
I need your help!!!  
I have to execute in Pre-Script Request an 8 digit dynamic variable and then apply a formula to that variable like this:  
Digit 1 \* 3  
Digit 2 \* 2  
Digit 3 \* 1  
Digit 4 \* 7  
Digit 5 \* 6  
Digit 6 \* 5  
Digit 7 \* 4  
Digit 8 \* 3  
The sum of all that, I divide by 11 and I get from the result only the first decimal

Is it possible to do that?

I already thank you

Greetings from Argentina

---

<div class="post-metadata">

**Author:** ![zeballosfederico1](https://avatars.discourse-cdn.com/v4/letter/z/90db22/32.png) [@zeballosfederico1](https://community.postman.com/u/zeballosfederico1)\
**Post date:** [August 1, 2020, 9:16pm UTC](https://community.postman.com/t/dynamic-variable-formula/14647/2 "2020-08-01T21:16:26Z")

</div>

alguien me ayuda? Help!!

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [August 3, 2020, 12:12pm UTC](https://community.postman.com/t/dynamic-variable-formula/14647/3 "2020-08-03T12:12:12Z")

</div>

While I feel like I may be doing your homework for you, this was still a fun little exercise.

```javascript
const number = getNumber(8);

let total = calculate(number, 0, 3);
total += calculate(number, 1, 2);
total += calculate(number, 2, 1);
total += calculate(number, 3, 7);
total += calculate(number, 4, 6);
total += calculate(number, 5, 5);
total += calculate(number, 6, 4);
total += calculate(number, 7, 3);

let quotient = total / 11;

const numbers = (quotient + '').split('.');
const digit = numbers.length < 2 ? 'N/A' : numbers[1][0];

pm.variables.set('number', number);
pm.variables.set('sum', total);
pm.variables.set('quotient', quotient);
pm.variables.set('firstDecimal', digit);

function getNumber(digits){
  let number = '';
  for(let i = 0; i < digits; i++){
    let digit = Math.ceil(Math.random()*9);
    while(number.length === 0 && digit === 0){
      digit = Math.ceil(Math.random()*9);
    }

    number += digit;
  }

  return number;
}

function calculate(number, index, multiplier){
  const digit = Number(number[index]);
  const value = digit * multiplier;

  return value;
}

```

If you’re echoing values in the request:

 ![image](https://us1.discourse-cdn.com/getpostman/original/2X/d/d80dba3330d336fd1d2cd63ea65d65da12453986.png)

Example Run:  
 ![image](https://us1.discourse-cdn.com/getpostman/original/2X/6/6107fd70ca85fdd992e4b31316a26ab2708ebce3.png)
