9. The N Shortcut 2


#1

Hi everyone, I'm having trouble with exercise 9, it keeps telling me that " It looks like the n key isn't moving to the next article. Remember to remove the 'current' class from currentArticle and place it on nextArticle."

But i can't figure out where is my mistake, so i hope you can help me out.

Here's my code :

var main = function() {
$('.article').click(function() {
$('.article').removeClass('current');
$('.description').hide();
$(this).addClass('current');
$(this).children('.description').show();
});
$(document).keypress(function(event) {
if (event.which === 111) {
$('.current').children('.description').toggle();
}
if (event.which === 110) {
}
else if (event.which ===110) {
var currentArticle = $('.current');
var nextArticle = currentArticle.next();

            currentArticle.removeClass('current');
            nextArticle.addClass('current');
        }
});

}
$(document).ready(main);


#2

Hi Alexis,

You need to remove that if statement. It's getting triggered when you press N, but then there's nothing it does, so nothing happens. But because that got triggered, nothing else in that chain gets triggered until you press the key again. But the same thing happens, because the empty if statement is before the else if statement with the code.


#3

Thanks a lot but the exercise itself told me to write that else if statement. I'll still try.

EDIT : It's not working...


#4

@overbox Would you mind posting your updated code, please?
Thanks.


#5

Oops sorry, I forgot to post it, but a few minutes later, I saw my mistake with my two if and elsif, I corrected it but forgot to tell you. Anyway, thanks a lot and enjoy your vacations !


#6

Can you explain it some other way?


#7

@nyema15 Sure, I can try :)
As the computer goes through the code, it looks for matches to the statement. So,

if (2 == 3) { // this *won't* get run, because 2 != 3
  // ...
}
else if (2 == 2) { // this *will* get run, because 2 == 2
  // ...
}

In that case, the second thing, else if (2 == 2) {...}, will get run. But if we have this instead:

if (2 == 3) { // this *won't* get run, because 2 != 3
  // ...
}
else if (2 == 2) { // this *will* get run, because 2 == 2
  // ...
}
else if (2 > 1) { // this *won't* get run, even though it's true, because 
                  // the computer stops at the first 'match'; the first
                  //  statement that evaluates to true, runs it, and stops
                  // going through the if/else if/else statement and 
                  // continues through the rest of the code
  // ...
}

The third thing, else if (2 > 1) {...}, won't be run, even though it's true, because the computer stops at the first thing that evaluates to true, runs the code inside it, and ignores the rest of the if/else if/else block and goes through the rest of the code.

Does that help you understand it?


#8

It really did! Thank you!!!