9. The N Shortcut 2


#1

Hi, I get this error message and I'm not sure why: SyntaxError: expected expression, got keyword 'else'

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();
        else if (event.which === 110) {
            var currentArticle = $('.current');
            var nextArticle = currentArticle.next();

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

}

$(document).ready(main);


#2

@giannamoglino
Make sure to close your curly brackets for the if statement and else if statement. I think the error tells you to add a else statement like every other if/else statement if there is a else if, there should be a else too. Look carefully at your code and at the instructions. Hope this helped you.


#3

I appreciate your reply :slightly_smiling: @miniapple8888. Would you mind showing me what the revised code would look like please? Thank you.


#4

@giannamoglino
Here is the code:

SPOILER!

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();
       }else if (event.which === 110) {
            var currentArticle = $('.current');
            var nextArticle = currentArticle.next();

            currentArticle.removeClass('current');
            nextArticle.addClass('current');
          }else{
            // Place to put code in else
         }
}

$(document).ready(main);


#5

@miniapple8888 Thank you. I get this error message when I used that code and cannot figure out why.

SyntaxError: missing ) after argument list


#6

Also this error message again:

Oops, try again.
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.


#7

@giannamoglino
Ahh, I think I see why. Before selecting your children .description to toggle it or under your if statement on whether the user pressed the n key, you must hide the class description like this; $('.description').hide();. Also, you must close your event handler with a curly bracket, a parentheses, and a semicolon. Here's the code you should have;

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) {
    $('.description').hide();
    $('.current').children('.description').toggle();
   } else if (event.which === 110) {
        var currentArticle = $('.current');
        var nextArticle = currentArticle.next();

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

});
}

$(document).ready(main);


#8

That worked :slightly_smiling: thanks @miniapple8888