Dynamic variable + Formula

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

alguien me ayuda? Help!!

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

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:

Example Run:
image

1 Like