There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
You can also find further discussion and get answers to your questions over in Language Help.
Agree with a comment or answer? Like () to up-vote the contribution!
Suppose the percentage of shared DNA is 12. According to the specifications, this should result in a statement of “first cousins”. But, this is not what will be printed if you use the code suggested by you.
The first if condition ($percentage === 100) will be FALSE, so the next condition will be checked.
The second condition ($percentage < 100) will be TRUE because indeed 12 is less than 100. So, the wrong message “parent and child or full siblings” will be printed.
You don’t need the elseif ($percentage < 100) condition. But even after deleting this particular condition, the other conditions also result in flawed logic. Instead consider using conditions involving the > or >= operator. That will help you in capturing the correct behavior.
If i assign to $dna_percentage a value of over 100 it prints a response: “parent and child or full siblings” (as it should according to the statements).I added an extra statement that prints “Not applicable” but i can see that it works only if a negative number is assigned: whatRelation(-12) → prints "Not applicable.
The question is how can i limit the value of $dna_percentage to maximum 100 so that it will also print "Not applicable when a value higher than 100 is printed.This is my code:
I found a way to do it.If i add this statement right after the “if” statement it can print a different value than “Not applicable” (“Value too high”).It is not exactly the same as i wanted so any help would still be appreciated.
elseif($dna_percentage > 100){
echo "Value too high";
}
Yes, the feedback isn’t quite correct because the instructions state “We expect the number passed in to always be an integer from 0 to 100”.
However, I would suggest that other than the first condition (the one with $grade > 99), for all the other conditions, you should use greater than or equal to i.e. you should use >= instead of >
For example the condition ($grade > 35) should be edited to ($grade >= 35) because the specifications state: 35 through 99 should print "parent and child or full siblings"
They probably tested your program with an input of 3. According the specs: 3 through 5 should print "second cousins". But since you used ($grade > 3), so the third cousins condition was invoked instead.
identical twins
parent and child or full siblings
grandparent and grandchild, aunt/uncle and niece/nephew, or half siblings
first cousins
second cousins
third cousins
not genetically related
I received n error message like:
Your function should print the string "identical twins" if invoked with 100. We tested your function with 100 and expected "identical twins" but instead it printed ""
I don’t explain what is it going on with the site unit test…
Write a function, whatRelation() that has one number parameter representing the percentage of DNA the two people share. Your function should print the likely relationship as a string.
In your code, you are returning strings and then printing them outside the function
function whatRelation($percent): string
{
if ($percent == 100) {
return "identical twins";
} else if (...
}
echo whatRelation(100);
According to the instructions, the function is supposed to print (not return) the result.
function whatRelation($percent)
{
if ($percent == 100) {
echo "identical twins";
} elseif (...
}
whatRelation(100);