Struggling with calculation and data interpretatio...
# 🤝help
a
I hope someone can help me with this, I've been trying to write some code that will take in my captured number which is a workflow variable called userID and return the age of the individual and their gender given that the userID is a South African ID number. Below is my code: However when testing I get no returns at all. The variables for clientAge and clientGender stay empty. In the execute code editor there is a slight grey underscore on clientid where it states: Paramter 'clientid' implicitly has an 'any' type, but a better type may be inferred from usage There are no other errors that occur or show up throughout the code that you would usually expect if you had a syntax error or were attempting to use an invalid or unrecognised function. I have limited coding and development experience and for the most part the code is written with the assistance of chatGPT. Really hoping someone can help me out here. let clientid = workflow.userID; function calculateAge(clientid) { // Extracting birth date components from the ID number const year = Number(clientid.substr(0, 2)); const month = Number(clientid.substr(2, 2)); const day = Number(clientid.substr(4, 2)); // Getting current date components const today = new Date(); const currentYear = today.getFullYear(); const currentMonth = today.getMonth() + 1; // Months are zero-based const currentDay = today.getDate(); // Calculating age let age = currentYear - year; // Adjusting age if birth month is greater than current month if (month > currentMonth) { age--; } // Adjusting age if birth month is equal to current month but birth day is greater else if (month === currentMonth && day > currentDay) { age--; } return age; } function determineGender(clientid) { const genderCode = Number(clientid.substr(6, 4)); if (genderCode < 5000) { return "Male"; } else { return "Female"; } } const clientAge = calculateAge; const clientGender = determineGender;
a
Hey @average-megabyte-82737 , the good news is that you're most of the way there! Try changing the last two lines to:
Copy code
workflow.clientAge = calculateAge(clientid)
workflow.clientGender = determineGender(clientid)
Basically, your code defines two funtions, calclateAge and determineGender, but these functions don't run automatically. You need to run them and provide the cliendid as input, and then set your workflow variables as to take the output.
a
@acceptable-kangaroo-64719 I tried that however I get this response: errorError executing action "inline-ins-aabeeed911.js" in flow:Client Recruitement:node:Client-Information1 [TypeError, clientid2.substr is not a function]
Also big thanks for helping by the way, this is a pretty cool community
a
that's good! It means your code is running, and all that's left to do is debug it 😆
I gave it a try on my end, and this code seemed to be working:
Copy code
let clientid = "9202204720082"; //Sample number

function calculateAge(clientid) {
  // Extracting birth date components from the ID number
  const year = Number(clientid.substr(0, 2));
  const month = Number(clientid.substr(2, 2));
  const day = Number(clientid.substr(4, 2));

  // Getting current date components
  const today = new Date();
  const currentYear = Number(today.getFullYear().toString().substr(-2));
  const currentMonth = today.getMonth() + 1; // Months are zero-based
  const currentDay = today.getDate();

  // Calculating age
  console.log(year, currentYear)
  let age = currentYear - year;
  if (age < 0){
    age = age + 100
  }

  // Adjusting age if birth month is greater than current month
  if (month > currentMonth) {
    age--;
  }
  // Adjusting age if birth month is equal to current month but birth day is greater
  else if (month === currentMonth && day > currentDay) {
    age--;
  }

  return age;
}

function determineGender(clientid) {
  const genderCode = Number(clientid.substr(6, 4));

  if (genderCode < 5000) {
    return "Male";
  } else {
    return "Female";
  }
}

workflow.clientAge = calculateAge(clientid)
workflow.clientGender = determineGender(clientid)
a couple things: * For Date, getFullYear returns a 4 digit year, while the ID number just has 2 digits. You have to do some extra maths to make sure someone born in 1992 is (2023-1992) years old and not (2023-92) years old * Running the functions at the bottom * I'm using a hard-coded SA ID number for testing purposes. You should probably do some validation on the user-inputted value to make sure it has enough of the right digits. I'd recommend regex
a
That code does indeed work even when I try hardcoding my ID, I see your point about ensuring that the code can handle people born both before and after Y2K. So essentially I need to code in some conditionall math for that and then find a way to populate clientid with my captured variable where I stored the userID
a
yup! I'd recommend using regex to validate the input like this
ChatGPT is really good at writing regex strings, I also like using https://regex101.com/
a
This is way better than what I was thinking of doing, was thinking of using Execute Code to do Luhn's algorithm after confirming length is 13 digits to confirm validity of the ID number but the regex is a much more elegant solution. Fewer process calls in the flow aswell
Interesting, the moment I try to feed userID using workflow.userID into clientid instead of hardcoding I get an error about a reference that isn't even in my code. Error states: errorError executing action "inline-ins-aabeeed911.js" in flow:Client Recruitement:node:Client-Information1 [TypeError, clientid2.substr is not a function]
however clientid2.substr appears nowhere in the code
a
Are you converting the numeric clientid into a string before running
.substr
?
a
I think that is part of the issue, so in the first funtion calculateAge which is passed clientid as a number, year (initialized as a number) is formed from doing clientid.subtr(0,2) however I suspect that because it is of type Number and not String this does not work. Perhaps a number way of doing it would be to store the clientid in an array, each of its digits taking up a slot in the array and then instead of doing substr populate year as an extraction of that array? Doing math would the array though is very over-engineered. Perhaps there is a number version of substr? Like a subnum? Based on my memories years ago of basic C++ I doubt this exists without a very specific kind of library being used
a
Luckily Javascript is a lot more flexible with datatype than C++. To turn an int to a string, you can just run
.toString()
So I'd try changing the first line to:
Copy code
let clientid = workflow.clientId.toString()
a
and then I would do the reverse post substr extraction to ensure that year is of datatype number that can be used in calculations right?
a
yes, that's already being done in the code
a
ahhhhhhhhhh because its already instead the Number(..............)
a
here
a
inside*
a
exactly 😉
a
I'm having more fun learning this language and system than I did playing the Destiny 2 vanilla campaign
a
W3 schools' Javascript page constantly saves my ass, it's my #1 place to go when I forget something about JS https://www.w3schools.com/js/js_strings.asp
a
Thanks for the tip, definitely going to keep this open on a seperate screen here!
very interesting, seems some string methods cannot be used after converting number to a string, doing some digging on the site
I got it to work, fixed it XD works on all examples. Incase anyone needs reference this is the finalized working code:
function calculateAge(clientid) { // Extracting birth date components from the ID number const year = Number(clientid.substr(0, 2)); const month = Number(clientid.substr(2, 2)); const day = Number(clientid.substr(4, 2)); // Getting current date components const today = new Date(); const currentYear = Number(today.getFullYear().toString().substr(-2)); const currentMonth = today.getMonth() + 1; // Months are zero-based const currentDay = today.getDate(); // Calculating age console.log(year, currentYear) let age = currentYear - year; if (age < 0){ age = age + 100 } // Adjusting age if birth month is greater than current month if (month > currentMonth) { age--; } // Adjusting age if birth month is equal to current month but birth day is greater else if (month === currentMonth && day > currentDay) { age--; } return age; } function determineGender(clientid) { const genderCode = Number(clientid.substr(6, 4)); if (genderCode > 5000) { return "Male"; } else { return "Female"; } } let clientid = workflow.userID.toString(); //Sample number workflow.clientAge = calculateAge(clientid) workflow.clientGender = determineGender(clientid)
a
Aweome 🙌 thanks for sharing the finished code!
a
As soon as I have a working alteration to account for people born after Y2K I will post that version aswell but for now this code works with all examples and stores the variables for use elsewhere within the workflow
@acceptable-kangaroo-64719 you're a champ thank you
a
This block of code should account for pre- and post- Y2K birth years. * If
currentYear
is 23 and
year
is 92, age is -69. This triggers the if/else so we add 100 to age to get a final number of 31 * If
year
is 02, then age is 21 and we don't trigger the if/else This code block also has the side effect of making you feel old 🥲
come to think of it, that
<
should be a
<=
so that if someone was born in 1923 we don't get an error
a
I saw that however when inputting with an ID where the birth year was 03 it stated that they were 92 years old
Agreed
a
probably something strange happens that isn't converting
03
to
3
a
functionally this should work, but still same error, I suspect something is being lost in how clientid is getting passed to the function but not sure about that. I'm going to see if I can run this same code in an IDE and see what it says, the more detail steplogs might give me more insite as to what is happening at each step
function calculateAge(clientid) { const currentYear = new Date().getFullYear(); const currentMonth = new Date().getMonth() + 1; const birthYearPrefix = clientid.charAt(0) === '0' ? 2000 : 1900; const birthYear = parseInt(clientid.substr(0, 2), 10); const birthMonth = parseInt(clientid.substr(2, 2), 10); const age = currentYear - (birthYearPrefix + birthYear); // Check if the birthday has already passed this year if (birthMonth > currentMonth) { return age - 1; } else if (birthMonth === currentMonth) { // Check if the birthday is in the current month and has already passed const birthDay = parseInt(clientid.substr(4, 2), 10); const currentDay = new Date().getDate(); if (birthDay > currentDay) { return age - 1; } } return age; }
a
that's a good idea, putting it in an IDE. Good luck!
165 Views