Javascript formula that will combine 3 fields into one

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.

// 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"

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.

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

return message;
2 Likes

Thanks @Jeff_Hager , that worked!

1 Like

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