Curly Brace Confusion in JavaScript

Okay Guys,

I am STUCK on the Functions & if/else in Codecademy. I have tried everything I can think of and I am really having difficulty with this problem. I stumbled upon this post by Dave Ward: http://bit.ly/jlRVVC. No doubt the article is intended for seasoned programmers, but I found it helpful nonetheless. I realize that the placement of curly braces can make or break your code. I am going to need to have a better understanding of this before moving forward. Anyone out there have any pointers or advice? Codecademy said it expected an identifier and instead saw “else.” It might look a little gross, but here’s my code. 

var creditCheck = function (income)
{
if (income >= 100);
return (“You earn a lot of money! You qualify for a credit card.”) };

{
else;
return (“Alas you do not qualify for a credit card. Capitalism is cruel like that.”);
}

(prompt (“How much money do you make?”));

creditCheck(75);
creditCheck(125);
creditCheck(100);

Thanks everyone! Hope your JS is going well! I would love to hear some more stories of your progress to keep me going!

About Nicole C.

Hi, I'm Nicole, and this is my story... After spending my entire adult life living in Los Angeles, I got a little antsy and did an 8 month stint in Austin Texas. I had some fun and earned some hard-won life lessons, but now I am back and I'm finally ready to call LA home. I just got married to a fun, sweet nerdy man last September, and we are going to start a family in the next little while. I'm a quirky artist who dreams of backyard dinner parties, crafting, cocktails and unicorns. I've also spent the last 15 years working in business administration, but it's time for a change. I am in the process of reinventing myself as a code princess, and launching myself into the much more promising and lucrative world of web development. I will settle for nothing less than the brightest of futures for me and my family, and I'm going to get there using my brains, determination, and Sass! Time to put on my big girl pants. Thanks for checking out my blog!

One response to “Curly Brace Confusion in JavaScript

  1. Give this a try:

    var creditCheck = function(income) {
      if (income >= 100) {
        return "You earn a lot of money! You qualify for a credit card.";
      } else {
        return "Alas you do not qualify for a credit card. Capitalism is cruel like that.";
      }
    }

    You can get away with just:

    var creditCheck = function(income) {
      if (income >= 100)
        return "You earn a lot of money! You qualify for a credit card.";
      else
        return "Alas you do not qualify for a credit card. Capitalism is cruel like that.";
    }

    But, the explicit curly braces around the if and else conditions helps to keep things clearly delineated, and are required if you expand the if or else block to more than one line.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: