Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

If statement not working

Condition is still resulting in "Clouds" and not "Puffy". What did I do wrong?

    function addWeather(icon, day, condition){

    if (condition == "Clouds"){
        condition = "Puffy";
    };

    var markup = '<li>'+
        '<img src="assets/img/icons/'+ icon +'.png" />'+
        ' <p class="day">'+ day +'</p> <p class="cond">'+ condition +
        '</p></li>';

    scroller.append(markup);
}

Probably the code working okay, but I don't think that semicolon after the closing curly bracket on the if statement is supposed to be there.

Richard - I believe the semicolon in this case is optional and the block will execute if the test evaluates to true. However, if you ran this through something like JSLint, you'd probably get a warning.

As I understand it, it is because the closing curly brace (}) ends the if statement. The trailing semicolon (;) indicates there is a second, "empty" statement on the line. It still works since empty statements are valid JavaScript, but you get the warning because it is unnecessary.

1 Answer

Try adding a console.log to show what the variable's value is prior to the if statement. This will help us clue in on what is going wrong. (I'm assuming you are using Chrome or Firefox with Firebug installed.)

function addWeather(icon, day, condition){
    console.log(condition);
if (condition == "Clouds"){
    condition = "Puffy";
};
...    

My initial guess is that the test in if is evaluating to false, so the code in the block is never run. We want to see what the value is, then determine why it is coming up false. Hope that helps!

Hi Brandon, Thanks for looking into this. There was more information in the array, so I had to dig the info I wanted out. If interested, you can follow this here: https://teamtreehouse.com/forum/weather-app-modification

I see. Looks like your question was already solved. :) Have a great day!