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."
4 Likes

These challenge should have a little more instructions… there is a few ways we could get to an output yet it wont be accepted. After 3 days of going over and rewriting the function… I cannot believe how obvious and simple it was it actually made me mad LOL.

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

Besides that. Using multiple if’s with the same indentation instead of else if’s seems bad code or bad pratice. Am i wrong?

Neither. It depends on whether return is used, or not. Concern yourself more with syntax and usage mechanics, and less with practices. When you are in a position to improve your own code, with deliberation, that will be the time to read a style guide to brush up on what are recommended best practices. You have enough on your plate, just now. Set this aside.

1 Like

This is the first challenge where I had to view the solution. I still have no idea why the solution works. I assumed the solution needed logical || / &&. Does it have something to do with exclusively using ‘if’ statements instead of ‘if else’?

Alright, I might be responding to my own question here, but after going over the solution, is it because the first case that is true is what is returned? Almost as if the computer is going through a sequential check list but not as efficient as an else if paired with heavy &&'s and ||'s, resulting in easier to read code? Admittedly I’m not quite sure about the efficient part, but is my thinking on the right track, here?

We don’t concern ourselves with efficiency at this level, especially where if statements are concerned. When return is used in the branches, we don’t need else.

1 Like

Thanks! Very helpful response. :call_me_hand:t6:

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

omg dude, one little more than that was all! my reaction “NO WAY” and with a little profanity to add to that lol

Hey everyone! I have a question about this code we are fixing and I’m wondering why in this code there are no semicolons used?

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

console.log(whatRelation(34))
// Should print 'You are likely grandparent and grandchild, aunt/uncle and niece/nephew, or half siblings.'

console.log(whatRelation(3))
// Should print 'You are likely 2nd cousins.'

From my googling, I understand that

JavaScript does not strictly require semicolons. When there is a place where a semicolon is needed, it adds it behind the scenes. This is called Automatic Semicolon Insertion.

So now I’m a bit stumped as when you must use them?

I also came across this article discussing the use of them after }.

Avoid Semicolons After ‘}’. Do not put a semicolon after a closing curly bracket ‘}‘. The only exception is an assignment statement like this: var data = {name: “Alice”, age: 25};

However, I felt like I’d seen }; being used continuously throughout the ‘Functions’ syllabus:

Helpful Functions

function multiplyByNineFifths(number) {
  return number * (9/5);
};
 
function getFahrenheit(celsius) {
  return multiplyByNineFifths(celsius) + 32;
};
 
getFahrenheit(15); // Returns 59

Arrow Functions

const rectangleArea = (width, height) => {
  let area = width * height;
  return area;
};

Concise Body Arrow Functions

const squareNum = (num) => {
  return num * num;
};

So yeah, now I’m a bit stumped :joy:

You can choose to use semicolons in a number of places …
but you you can’t put semicolons in between an if-block and an else block (or else-if block).

The code below would cause an error.

let sky = "gray";

if (sky == "blue") {
  console.log("The sky is blue");
};  // the ; here will cause an error
else {
  console.log("The sky is not blue now, it is " + sky);
}