FAQ: Debugging JavaScript Code - Debugging with console.log()

This community-built FAQ covers the “Debugging with console.log()” 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 with console.log()

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!

Can someone please explain why ‘a’ is less than ‘b’? I think this has something to do with lexicographical order but I couldn’t really find a proper source of information about that.

3 Likes

It has to do with ordinal. The character code for a is 97 and for b, 98.

Think of it in terns of a comes before b when examining the < relation.


'A' < 'a'

is also true. It follows, since 65 is less than 97.

4 Likes

Thank you so much for your response. Is there a link you would recommend that explains ordinal well? I’ve been searching but it’s so broad that I’m finding it difficult to understand why ‘a’ is 97 and ‘b’ is 98. I also don’t understand why ‘A’ is 65, but I’d like to know for the future.

3 Likes

An ordinal is the position number in a character set. One of the earliest is ASCII, and in Windows it would be encoded as Windows-1252.

Today we use what is largely universal, the international UTF-8 encoding for web documents and applications.

ASCII, decimal 65  =>  'A'  <=  hex '\u0041'

ASCII, decimal 97  =>  'a'  <=  hex '\u0061'

Eg.

 > A = 0x0041
<- 65
 > String.fromCharCode(A)
<- "A"
 > a = "\u0061"
<- "a"
5 Likes

Thank you so much for the links!!! The printable character list was exactly what I was looking for. I’ve never heard of any of this until now. It would have been nice information to be aware of going into the lesson, haha. Oh well, I’m beyond grateful to have it now, thanks again.

1 Like

You’re welcome. Here is another page I just dug up to augment this study…

What every JavaScript developer should know about Unicode

I’m digging into it right now, and expect you will be soon. Happy coding!

One supposes that was never considered. There are what would be best described as holes in learners’ knowledge base on any number of elementary concepts, as presented. On review it will be advisable to seek these out and address them in an appendix. The idea has crossed my mind several times, and perhaps now that the forum revamp is nearly complete, we can start one. Learners are often reporting that concepts are foreign. An appendix would be a place to find those concepts, and then resume the lesson with a better bearing.

Unlike a glossary, an appendix can actually explore concepts and let them play out. This let’s the reader leave with a package, not a definition.

7 Likes

An appendix sounds like a wonderful idea! Thank you for the extra link, I will definitely did into that as well :slight_smile:

I don’t particularly know what I’m supposed to “print” inside the if/else block. I was able to find the bug but just by staring at the code :frowning:

If I wanted to print something to the console that would have shown me where the error in the logic is, would I just add a console.log(string1)?

5 Likes

I wrote like this:

if (firstLetter1 === firstLetter2) {
    console.log(firstLetter1 === firstLetter2);
    return null;
  } else if (firstLetter1 > firstLetter2) {
    console.log(firstLetter1 >firstLetter2);
    return string1;
  } else {
    console.log(firstLetter1 < firstLetter2);
    return string2;
  }
6 Likes

The hints didn’t really help me here. I did not understand where I what I was supposed to type in order to find the error in the logic.

I first tried to log the value of firstLetter1 outside of the function and that just gave me a reference error. I next tried to log the values of firstLetter1 and 2 after their appropriate strings should they meet the conditions of the if/else block. That got me nowhere even though that was the method used in the previous example. After much frustration I just viewed solution, unfortunately.

But surely the solution could have been something I could have found if I had just stared at the code a bit longer. My hunch was that .charAt(1) is not the first character, going by what I learned about indexes at this point.

Here it seemed that the instructions and hints were cryptic and what worked in the previous lesson was of no help here.

1 Like

Good exercise. Worth noting/reminding something that stumped me briefly - if you’re using a console.log() within the if/else block, make sure you do this BEFORE the return statement. If you put if after, nothing extra will be logged because return ends the function.

4 Likes

I wrote my code like this and worked perfectly fine:

function getLaterFirstLetter(string1, string2) {
const firstLetter1 = string1.charAt(0);
const firstLetter2 = string2.charAt(0);

if (firstLetter1 === firstLetter2) {
console.log(firstLetter1 === firstLetter2);
return null;
} else if (firstLetter1 > firstLetter2) {
console.log(firstLetter1 > firstLetter2)
return string1;
} else {
console.log(firstLetter1 < firstLetter2);
return string2;
}
}
// Should return “blueberry”
console.log("getLaterFirstLetter(‘avocado’, ‘blueberry’) returns: " + getLaterFirstLetter(‘avocado’, ‘blueberry’));

// Should return “zebra”
console.log("getLaterFirstLetter(‘zebra’, ‘aardvark’) returns : " + getLaterFirstLetter(‘zebra’, ‘aardvark’));

// Should return null
console.log("getLaterFirstLetter(‘astro’, ‘afar’) returns: " + getLaterFirstLetter(‘astro’, ‘afar’));

Output:

true
getLaterFirstLetter(‘avocado’, ‘blueberry’) returns: blueberry
true
getLaterFirstLetter(‘zebra’, ‘aardvark’) returns : zebra
true
getLaterFirstLetter(‘astro’, ‘afar’) returns: null

Please let me know if I did it correctly, although the solution gave me the answer to change ‘>’ to ‘<’
} else if (firstLetter1 < firstLetter2) {
return string2;
} else {
return string1;