Adding JAVA Script code to determine Life Path calculation

I have tried to do the Math Calculation but this is a bit complex to work out in Glide.

function getLifePath(dob) {
const digits = dob.replace(/-/g, ‘’).split(‘’).map(Number);
const reduce = n => {
if ([11, 22, 33].includes(n)) return n;
while (n > 9) {
n = n.toString().split(‘’).reduce((a, b) => a + Number(b), 0);
}
return n;
};

const date = new Date(dob);
const day = date.getDate();
const month = date.getMonth() + 1;
const year = date.getFullYear();

const daySum = reduce(day);
const monthSum = reduce(month);
const yearDigits = year.toString().split(‘’).map(Number);
const yearSum = reduce(yearDigits.reduce((a, b) => a + b, 0));

const total = reduce(daySum + monthSum + yearSum);
return total;
}

return getLifePath(p1); // p1 = Date of Birth input

You posted this in Ask for Help. Did you have a question?

Yes code I got from ChatGPT to use with glide Java script however it will not compile. I am trying to calculate a life path number which is basically adding up all the numbers in your date of birth as single digits until you are left with 2 digits. If the remaining digits are 11 or 22 or 33 these are master number and are not added together. If the remaining digitals are not these above you add them into a life path number.

Life Path Number Numerology Calculator: Meanings In Life & Love

What error do you get? What does the DOB value look like that you passing into the P1 parameter?

Please try this.

function calculateLifePathNumber(zuluDate) {
  const date = new Date(zuluDate);
  const month = date.getUTCMonth() + 1;
  const day = date.getUTCDate();
  const year = date.getUTCFullYear();

  const sumDigits = n => n.toString().split('').reduce((a, b) => a + Number(b), 0);

  const monthValue = month;
  const dayValue = sumDigits(day);
  const yearValue = sumDigits(year);

  return monthValue + dayValue + yearValue;
}

return calculateLifePathNumber(p1)

With p1 being the DOB.

1 Like

Here’s a full thing packed in a Math column.

MONTH(D)
+ FLOOR(DAY(D) / 10) + MOD(DAY(D), 10)
+ FLOOR(YEAR(D) / 1000)
+ FLOOR(MOD(YEAR(D), 1000) / 100)
+ FLOOR(MOD(YEAR(D), 100) / 10)
+ MOD(YEAR(D), 10)

Thank you very much I have it working down to the 2 digits, however if the life path is not a Master Number (11, 22, 33) it should be added together for example 36 should be 3 + 6 = 9

function calculateLifePathNumber(zuluDate) {
  const date = new Date(zuluDate);
  const month = date.getUTCMonth() + 1;
  const day = date.getUTCDate();
  const year = date.getUTCFullYear();

  const sumDigits = n => n.toString().split('').reduce((a, b) => a + Number(b), 0);

  const monthValue = month;
  const dayValue = sumDigits(day);
  const yearValue = sumDigits(year);

  let total = monthValue + dayValue + yearValue;

  const isMasterNumber = n => n === 11 || n === 22 || n === 33;

  if (!isMasterNumber(total)) {
    while (total > 9) {
      total = sumDigits(total);
      if (isMasterNumber(total)) break;
    }
  }

  return total;
}

return calculateLifePathNumber(p1)

Let me know if this looks correct.

1 Like

LEGEND THANKS :clap: :clap: :clap:

Hi @simonpate and welcome! :slight_smile:

JavaScript is a powerful tool for solving tricky problems in Glide. I’m not sure this case is one of them though, since it’s mostly just arithmetic and logic.
Still, I have to admit the way the calculation works presents some interesting challenges :smirking_face:

Here’s a step-by-step approach, using… 13 columns in addition to the Date of birth column.

Expected result


Walkthrough

  1. Create 3 columns to isolate the parts of Date of birth:

    • Day (Math), with the formula DAY(dob)
    • Month (Math), with the formula MONTH(dob)
    • Year (Math), with the formula YEAR(dob)


  1. To calculate the sum of digits (e.g. : 2025 must become 2+0+2+5 = 9), let’s add 3 other columns, Math again, one for each part of the date:

    • Digital Root Day (Math), with the formula 1 + MOD(d - 1, 9). Replace d with the column Day
    • Digital Root Month (Math). Same with the Month column.
    • Digital Root Year (Math). Same with the Year column.

    This formula provides exactly the expected result, transforming any number of any length into a single-digit value, which is the sum of all the digits of the number repeated until only one digit remains. No looping needed here — it’s a neat arithmetic trick for calculating the so-called Digital Root. :smiley:


  1. Despite this nice trick, we need to check if the sum of digits for year is not 11 or 22. Prior to that, let’s add a new Math column. Let name it Sum Digits Year and use this calculation:
    MOD(y,10)
    + (MOD(y / 10,10) - MOD(MOD(y / 10,10), 1))
    + (MOD(y / 100,10) - MOD(MOD(y / 100,10), 1))
    + (MOD(y / 1000,10) - MOD(MOD(y / 1000,10), 1))
    

  1. Now it’s time to get the result for each part of the date that deals with values such as 11, 22 or 33. Let’s create 3 new columns, this time If → Then → Else ones:

    Thanks to these colmuns, if the values is 11, 22 or 33, we keep this value without simplifying it. Which is, as far as I now know, the right way to apply the calculation…


  1. We can now sum all the parts we have computed. Create a Math column Digital Root Sum (all digits) to achieve this. The formula is ny + nm + nd and here is the screenshot:

  1. Some sums are greater than 10 so we need to reduce them. Once again, let’s add a Math colmun to apply the same logic we have already used:
    Digital Root Sum (1 digit)

  1. And it’s almost the end. Last step is to define which value we get at the end, depending on the result. Either we obtain 11, 22 or 33 and it must stay that way, or we’ll have to Digital Root. It’s an If → Then → Else column, with a set up close to what we have done before:
    Life Path Number

As you can see in the first screenshot, this approach seems to work well.
That said, I’m not very familiar with the purpose of this calculation, so I might have missed something :thinking:
Also, here is the source I used to help me with this answer: https://www.tokenrock.com/numerology/life-path-number-calculator/

Could you give it a try and let us know if it works for you too?

4 Likes

Thank you all so much I really wanted the calculation in a java script so that I can have the calculation working for the particular field instead of adding so many columns and calcuations into my tables to get the result.

On a similar topic I am trying to determine the zodiac sign based on the DOB, please note dd/mm/yyyy field.

I used this code but it does not work I keep getting Capricorn for every date entered so some logic must be wrong to exit the if once found.

function zodiacSign(p1) {
var parts = p1.split(“/”);
var day = parseInt(parts[0], 10);
var month = parseInt(parts[1], 10);

if ((month === 1 && day >= 20) || (month === 2 && day <= 18)) return “Aquarius”;
if ((month === 2 && day >= 19) || (month === 3 && day <= 20)) return “Pisces”;
if ((month === 3 && day >= 21) || (month === 4 && day <= 19)) return “Aries”;
if ((month === 4 && day >= 20) || (month === 5 && day <= 20)) return “Taurus”;
if ((month === 5 && day >= 21) || (month === 6 && day <= 20)) return “Gemini”;
if ((month === 6 && day >= 21) || (month === 7 && day <= 22)) return “Cancer”;
if ((month === 7 && day >= 23) || (month === 8 && day <= 22)) return “Leo”;
if ((month === 8 && day >= 23) || (month === 9 && day <= 22)) return “Virgo”;
if ((month === 9 && day >= 23) || (month === 10 && day <= 22)) return “Libra”;
if ((month === 10 && day >= 23) || (month === 11 && day <= 21)) return “Scorpio”;
if ((month === 11 && day >= 22) || (month === 12 && day <= 21)) return “Sagittarius”;
return “Capricorn”;
}

return zodiacSign(p1);

What you need to be aware of is that what you see in the Data Editor is not necessarily the same as the actual stored value. This is particularly true with dates.

Try this: double click on one of the values in your DOB column. I would bet that what you see is completely different to the displayed value. Probably something like 1977-02-03T00:00:00Z (depending on how the value was entered). It is this value that is passed to the JavaScript column, not the displayed value. So most likely, the following lines are not returning what you expect:

To fix it, you most likely need to adjust those.

2 Likes

Try this.

function getZodiacSign(dob) {
  const date = new Date(dob);
  const day = date.getUTCDate();
  const month = date.getUTCMonth() + 1;

  const zodiac = [
    { sign: "Capricorn", from: [12, 22], to: [1, 19] },
    { sign: "Aquarius", from: [1, 20], to: [2, 18] },
    { sign: "Pisces", from: [2, 19], to: [3, 20] },
    { sign: "Aries", from: [3, 21], to: [4, 19] },
    { sign: "Taurus", from: [4, 20], to: [5, 20] },
    { sign: "Gemini", from: [5, 21], to: [6, 20] },
    { sign: "Cancer", from: [6, 21], to: [7, 22] },
    { sign: "Leo", from: [7, 23], to: [8, 22] },
    { sign: "Virgo", from: [8, 23], to: [9, 22] },
    { sign: "Libra", from: [9, 23], to: [10, 22] },
    { sign: "Scorpio", from: [10, 23], to: [11, 21] },
    { sign: "Sagittarius", from: [11, 22], to: [12, 21] }
  ];

  for (let i = 0; i < zodiac.length; i++) {
    const { sign, from, to } = zodiac[i];
    if (
      (month === from[0] && day >= from[1]) ||
      (month === to[0] && day <= to[1])
    ) {
      return sign;
    }
  }
  return "";
}

return getZodiacSign(p1)
2 Likes

Awesome thank you so much, I got it working with a few more columns but this is perfect.

:clap: :clap: :clap:

1 Like