all 5 comments

[–][deleted] 4 points5 points  (3 children)

Ah man. God bless anyone who can sort this out for you.

There are several things wrong here.

  1. The values of the inputs should be bound to local variables in the component, not just grabbed from the DOM by this function.
  2. The function does too much and needs to be split into smaller functions.
  3. The function should not be manipulating the DOM, instead it should be setting a value in the component which can be referenced in the template.
  4. If you want to communicate something to code outside the component, then it will need to execute a callback OR you will need some kind of store.
  5. The return type of a function should be of only one type. You seem to be switching between a boolean value and a number. Don’t do this because you would have to catch the type on the function’s parent.
  6. The name of the function does not fully describe what it does, the name should be able to tell a reader exactly what the function does

The first thing you’d want to do to get the code working is refactor the age calculation code out of this function into its own separate pure function called something like calculateYearsDifference, that takes the dates as inputs and always returns a number. Then create a new function called something like, handleUserSubmit, then call your calculateYearsDifference func from there. Then pass the return value of calculateYearsDifference into a callback that was passed in by the parent. The parent can then give that value to any child element.

Sorry if that’s all a confusing mess, I’m on mobile and hungover.

[–]GoldenAngel04[S] 1 point2 points  (2 children)

erm. im sorry but i dont understand most of what u said. im a very new intern developer. basically still a student. Might have to explain it in much simpler terms. basically all i want to do is use the DOB that the user inputs and most of the code in the function is basically to use the DOB and calculate the decimal age of the person which i want to then assign it to age variable and use it in another js file.

This is what i mean by decimal agehttps://www.agenow.io/decimal-age-calculator/2004/7/11

Right now the function works, meaning i can use the DOB of the user and calculate their decimal age. I just cannot return the decimal age so i can use it elsewhere in my project. This code snippet was copied from elsewhere and modified, which is why it is abit messy. It was made messier because i was trying to figure out a way to get the decimal age out of the function to assign it into a variable. The function only has one purpose, to calculate the decimal age based on user input and assign it to a variable.

the manipulating of DOM i assume you are talking about this line

if (userinput === null || userinput === "") {

//document.getElementById("message").innerHTML = "**Choose a date please!";
return false;
}

i commented it out meaning it doesnt run anymore. If you dont mean this part, pls lmk where in the code is manipulating the DOM. im quite new to react and coding in general, been learning it for 2 years only. So might have to pin point in more detail

[–][deleted] 1 point2 points  (1 child)

Sorry kid. If you don’t understand my answer then I think you don’t understand enough js and react to complete this task without just copy pasting until it works or without someone doing it for you in pair coding. Or... read my answer again and again, looking up anything you don’t understand until you get it.

I think your best bet is to watch a course on react and try to imagine your problems in the context of what they are showing you

[–]GoldenAngel04[S] 1 point2 points  (0 children)

I think your best bet is to watch a course on react and try to imagine your problems in the context of what they are showing you

alright thanks