FAQ: Debugging JavaScript Code - Debugging Review

This community-built FAQ covers the “Debugging Review” exercise from the lesson “Debugging JavaScript Code”.

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

Web Development

FAQs on the exercise Debugging Review

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 (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 (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!

function isStringPerfectLength(string, minLength, maxLength) {
const stringLength = string.length;
if (stringLength < minLength) {
return false;
} else if (stringLength > maxLength) {
return false;
} else {
return true;
}
};

The code is correct and doesn’t show errors, but just wondering why the last closing bracket is red?

I think if you have a scenario where string.length === minLength or maxLength works
so your method might work but it says if string.length is equal to or greater than min while also less than or equal to max then is true. may be the coding practices design for the exercise will want you to use && comparison to help further write short codes
try using using this:
function isStringPerfectLength(string, minLength, maxLength) {
const stringLength = string.length;
if (stringLength >= minLength && stringLength <= maxLength) {
return true;
} else {
return false;
}
}

Why should the first “if” be the false case, and the second “if” be the true case in order for the false cases to be computed?
This code worked:
function isStringPerfectLength(string, minLength, maxLength) {
const stringLength = string.length;

if (string.length < minLength || string.length > maxLength) {
return false;
} else {
return true;
}
}
// Output:
isStringPerfectLength(‘Dog’, 2, 4) returns: true
isStringPerfectLength(‘Mouse’, 2, 4) returns: false
isStringPerfectLength(‘Cat’, 4, 9) returns: false

Whereas this code didn’t work:
function isStringPerfectLength(string, minLength, maxLength) {
const stringLength = string.length;

if (string.length > minLength || string.length < maxLength) {
return true;
} else {
return false;
}
}
// Output:
isStringPerfectLength(‘Dog’, 2, 4) returns: true
isStringPerfectLength(‘Mouse’, 2, 4) returns: true
isStringPerfectLength(‘Cat’, 4, 9) returns: true

Why isn’t the logic working equally in both directions?

I also remembered there is another way to write the solution for this using the ternary operator, hope it helps somebody:

function isStringPerfectLength(string, minLength, maxLength) {

const stringLength = string.length;

return(stringLength >= minLength && stringLength <= maxLength? true : false)

}

1 Like

This was similar to mine!

However I remember that comparison operators return boolean values in itself – so that we don’t have to explicitly specify ? true:false.

So, more concisely:

function isStringPerfectLength(string, minLength, maxLength) {
  const stringLength = string.length;
  return (minLength < stringLength && stringLength < maxLength)}

True! good pointing that out. :ok_hand:

I solved this the wrong way and got a shorter if/else as a result

 if (stringLength > minLength && stringLength < maxLength) {
    return true;
  } else {
    return false;
  }
}

That works right?

I know the solotion does too

I see some other great examples in the replies above! Need to practice my ternary skills.

1 Like

FAQ: How about Chat GPT debugging?