Javascript code is not running

I’m using Javascript column but it is not giving result in Glide, already tested in compiler. It works.

I’m on free version of Glide, if this is an issue, which plan should get?

The purpose is, to get selected amout of monthes starting from month selected.

For example. 6 monthes from November 2023

The result should be. “November 2023, December 2023, January 2024, February 2024, March 2024, April 2024”

The code

  const monthNames = [
    "January", "February", "March", "April", "May", "June",
    "July", "August", "September", "October", "November", "December"
  ];

  let startIndex = monthNames.indexOf(startMonth);
  let currentYear = startYear;
  let resultMonths = [];

  for (let i = 0; i < monthsCount; i++) {
    let currentIndex = (startIndex + i) % 12;
    // Increment the year when the month wraps around to January
    if (currentIndex === 0 && i !== 0) {
      currentYear++;
    }
    resultMonths.push(monthNames[currentIndex] + " " + currentYear);
  }

  return resultMonths.join(", ");
}

// Example usage:
const startMonth = "November";  // Starting month
const startYear = 2023;         // Starting year
const monthsCount = 6;          // Number of months to list

const result = listMonths(startMonth, startYear, monthsCount);
console.log(result); // Correct Output: "November 2023, December 2023, January 2024, February 2024, March 2024, April 2024"

I haven’t tried your code but probably because you’re not returning anything. Change your last line to a return instead of console.log

1 Like

Issues:

  • You call a function that doesn’t exist (perhaps you didn’t share the entire code snippet?)
  • You don’t return anything.

Here is a working version:

function listMonths(startMonth, startYear, monthsCount) {
  const monthNames = [
    "January", "February", "March", "April", "May", "June",
    "July", "August", "September", "October", "November", "December"
  ];

  let startIndex = monthNames.indexOf(startMonth);
  let currentYear = startYear;
  let resultMonths = [];

  for (let i = 0; i < monthsCount; i++) {
    let currentIndex = (startIndex + i) % 12;
    // Increment the year when the month wraps around to January
    if (currentIndex === 0 && i !== 0) {
      currentYear++;
    }
    resultMonths.push(monthNames[currentIndex] + " " + currentYear);
  }

  return resultMonths.join(", ");
}

const startMonth = "November";  // Starting month
const startYear = 2023;         // Starting year
const monthsCount = 6;          // Number of months to list

return listMonths(startMonth, startYear, monthsCount);
3 Likes

Thank you Darren the code is working well :partying_face::sunglasses:

Thanks Eric

1 Like

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