# Javascript formula that will combine 3 fields into one

**URL:** <https://community.glideapps.com/t/javascript-formula-that-will-combine-3-fields-into-one/75411>\
**Category:** Ask for Help\
**Tags:** custom-code\
**Created:** [August 4, 2024, 3:34pm UTC](https://community.glideapps.com/t/javascript-formula-that-will-combine-3-fields-into-one/75411 "2024-08-04T15:34:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jarvis](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/jarvis/32/81377_2.png) [@Jarvis](https://community.glideapps.com/u/Jarvis)\
**Post date:** [August 4, 2024, 3:34pm UTC](https://community.glideapps.com/t/javascript-formula-that-will-combine-3-fields-into-one/75411/1 "2024-08-04T15:34:20Z")

</div>

Hi, I’m trying to replicate an spreadsheet IF/ELSE formula in Glide, that will combine three columns into a single text column. For clarification, I’ve put a picture of those 3 columns, and a text column on the right to show the desired output.

```auto
// Assuming you have three columns: staticsRemaining, videosRemaining, otherContentsRemaining
function remainingContentMessage(staticsRemaining, videosRemaining, otherContentsRemaining) {
  let result = [];

  // Check statics
  if (staticsRemaining > 0) {
    result.push(staticsRemaining + " " + (staticsRemaining === 1 ? "static" : "statics"));
  }

  // Check videos
  if (videosRemaining > 0) {
    result.push(videosRemaining + " " + (videosRemaining === 1 ? "video" : "videos"));
  }

  // Check other contents
  if (otherContentsRemaining > 0) {
    result.push(
      otherContentsRemaining + " " + (otherContentsRemaining === 1 ? "other content" : "other contents")
    );
  }

  // Construct final message based on conditions
  if (result.length === 0) {
    return "No content remaining";
  } else if (result.length === 1) {
    return result[0] + " remaining";
  } else {
    return result.slice(0, -1).join(", ") + " and " + result.slice(-1) + " remaining";
  }
}

// Example usage
const staticsRemaining = 3;
const videosRemaining = 1;
const otherContentsRemaining = 0;
const message = remainingContentMessage(staticsRemaining, videosRemaining, otherContentsRemaining);
console.log(message); // Output: "3 statics and 1 video remaining"

```

---

<div class="post-metadata">

**Author:** ![Jeff\_Hager](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/jeff_hager/32/43_2.png) [@Jeff\_Hager](https://community.glideapps.com/u/Jeff_Hager)\
**Post date:** [August 4, 2024, 3:54pm UTC](https://community.glideapps.com/t/javascript-formula-that-will-combine-3-fields-into-one/75411/2 "2024-08-04T15:54:40Z")

</div>

Your javascript doesn’t return anything. It’s just writing the result to the console log. Also looks like you aren’t passing any parameters from the table. I would make the following change and make sure you are passing in the appropriate p1, p2, and p3 parameters.

```auto
// Example usage
const staticsRemaining = p1;
const videosRemaining = p2;
const otherContentsRemaining = p3;
const message = remainingContentMessage(staticsRemaining, videosRemaining, otherContentsRemaining);

return message;

```

---

<div class="post-metadata">

**Author:** ![Jarvis](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/jarvis/32/81377_2.png) [@Jarvis](https://community.glideapps.com/u/Jarvis)\
**Post date:** [August 4, 2024, 4:19pm UTC](https://community.glideapps.com/t/javascript-formula-that-will-combine-3-fields-into-one/75411/3 "2024-08-04T16:19:18Z")

</div>

Thanks @Jeff_Hager , that worked!

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/system/32/53398_2.png) [@system](https://community.glideapps.com/u/system)\
**Post date:** [August 11, 2024, 4:19pm UTC](https://community.glideapps.com/t/javascript-formula-that-will-combine-3-fields-into-one/75411/4 "2024-08-11T16:19:44Z")

</div>

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
