var height = payload.height / 100; // Converting height to meters
var weight = payload.weight;
var bmi = weight / (height * height);

if (bmi < 18.5) {
  payload.suggestedSize = 'Your suggested size: XS';
} else if (bmi >= 18.5 && bmi < 24.9) {
  payload.suggestedSize = 'Your suggested size: S';
} else if (bmi >= 25 && bmi < 29.9) {
  payload.suggestedSize = 'Your suggested size: M';
} else if (bmi >= 30) {
  payload.suggestedSize = 'Your suggested size: L';
}

In this example, we use Execute JavaScript to send size recommendations to the client based on their weight and height. This functionality is especially useful for businesses working with clothing where clients have doubts regarding the most suitable size.

  1. First, in the automation flow, we request the client to provide their height and weight.
  1. Next, we save the client’s responses in the variables payload.height and payload.weight.
  1. In Execute JavaScript, we use the variables payload.height and payload.weight to calculate the client’s BMI and suggest the most appropriate size based on this information.
  1. Finally, the result of the code will be saved in the variable payload.suggestedSize, which is sent to the client in the last message indicating the suggested size based on their height and weight.
var height = payload.height / 100; // Converting height to meters
var weight = payload.weight;
var bmi = weight / (height * height);

if (bmi < 18.5) {
  payload.suggestedSize = 'Your suggested size: XS';
} else if (bmi >= 18.5 && bmi < 24.9) {
  payload.suggestedSize = 'Your suggested size: S';
} else if (bmi >= 25 && bmi < 29.9) {
  payload.suggestedSize = 'Your suggested size: M';
} else if (bmi >= 30) {
  payload.suggestedSize = 'Your suggested size: L';
}