FAQ: Code Challenges: JavaScript Fundamentals - Fix the broken code (round 2)!

This community-built FAQ covers the “Fix the broken code (round 2)!” exercise from the lesson “Code Challenges: JavaScript Fundamentals”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

FAQs on the exercise Fix the broken code (round 2)!

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Why doesn’t this code use else if statements? The options are mutually exclusive, so I don’t understand why we would want the computer to go through every option, even after it finds the one that is true.

It will only go through as many statements as needed to reach a true, at which point it returns. If the first one is true, the function is exited immediately.

I’m wondering if the only fix is in the line ‘(percentSharedDNA < 13)’ switching it to a ‘>’ because even copy/pasting the solution given if you reset the activity results in it not working. In the given solution there is an extra ‘n’ it seems in the line ‘if (npercentSharedDNA > 0)’

My code has all relational expressions set to greater than (>).

Hi y’all, I was wondering if anyone can explain to me why the below switch control flow is incorrect:

switch (percentSharedDNA) {

case (percentSharedDNA === 100):
    return 'You are likely identical twins.';

break;
case (percentSharedDNA > 34):
return ‘You are likely parent and child or full siblings.’;
break;
case (percentSharedDNA > 13):
return ‘You are likely grandparent and grandchild, aunt/uncle and niece/nephew, or half siblings.’;
break;
case (percentSharedDNA > 5):
return ‘You are likely 1st cousins.’;
break;
case (percentSharedDNA > 2):
return ‘You are likely 2nd cousins.’;
break;
case (percentSharedDNA > 0):
return ‘You are likely 3rd cousins’;
break;
default:
return ‘You are likely not related.’;
}

it keeps going down to the default results, without stopping at the correct case. Any idea?

The value of percentSharedDNA will never match the case expressions since they are boolean, consequently the switch will always return the default.

switch(true) {

}

Now the expressions can match the switch argument.

Hello, I would like to ask why my code gave syntax error. (SyntaxError: Unexpected token >=) Thank you anyone who would give time and attention to my question.

const whatRelation = percentSharedDNA => {
    if (percentSharedDNA === 100) {
        return 'You are likely identical twins.'
    }
    if (percentSharedDNA >= 35 && <= 99) {
        return 'You are likely parent and child or full siblings.'
    }
    if (percentSharedDNA >= 14 && <= 34) {
        return 'You are likely grandparent and grandchild, aunt/uncle and niece/nephew, or half siblings.'
    }
     (percentSharedDNA >= 6 && <= 13) {
        return 'You are likely 1st cousins.'
    }
    if (percentSharedDNA >= 3 && <= 5) {
        return 'You are likely 2nd cousins.'
    }
    if (percentSharedDNA >= 1 && <= 2) {
        return 'You are likely 3rd cousins'
    }
    if (percentSharedDNA === 0)
        return 'You are likely not related.'
}

One often points out how difficult it makes things when we try to distinguish betweenness. Given all the values are on a descending scale, all we need is a drop-down approach that eliminates one value at a time.

 psd = percentSharedDNA

if (psd < 1) {

}
if (psd < 3) {

}
if (psd < 6) {

}
if (psd < 14) {

}
if (psd < 35) {

}
if (psd < 100) {

}
return "You are likely identical twins."
1 Like