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


Sarah Bradberry
Sarah Bradberry
7,115 Points

If statement not running properly

I have updated a javascript I wrote with jquery so I can make it display in an html page.

Everything is working correctly except for one of my if statements.

Here are the lines within my form that should input the data for whether the user wants the results in inches or cm (it's a knitting pattern)

             <input type="radio" id="inches" name="increments">
             <label for="radio">inches </label>
             <input type="radio" id="cm" name="increments">
             <label for="radio">cm</label>

and here is what I have in my code

var     cm = $('input[id="cm"]').val(),
    inches = $('input[id="inches"]').val();

and to decide which to use

if (inches === true) {
var measure = "inches",
    handInches = parseFloat(handCircumference) * 2.5,
    thumbInches = parseFloat(thumbCircumference) * 2.5,
    thumbBase = parseFloat(thumbBaseToIndex),
    palm = (Math.round(thumbBase - .5) * 2 / 2).toFixed(1);} else {
var measure = "cm",
    handInches = parseFloat(handCircumference),
    thumbInches = parseFloat(thumbCircumference),
    thumbBase = parseFloat(thumbBaseToIndex),
    palm = (Math.round(thumbBase - 1.25) * 2 / 2).toFixed(1);

it is always using the else statement, even if the radio button for inches is chosen.

3 Answers

Jonas Olausson
Jonas Olausson
9,161 Points

Hi Sarah,

It seems like you haven't set a value to the input fields. For your code to work the val would have to be set to true for the inches field and false for the cm field.

I'm guessing that this isn't all of the code since you have some variables in the if statement that isn't defined, but you need to wrap that if statement in a change function or something like that so that it's fired once someone change/select a radio button.

I've made a small jsfiddle example that you can have a look at: http://jsfiddle.net/aS9zm/

Best regards Jonas

Have you tried using if(inches == true) ? Using triple-equals might be a bit too specific- that checks for if inches is exactly the same as true, instead of checking whether inches evaluates as true.

Sarah Bradberry
Sarah Bradberry
7,115 Points

Thanks Jonas and Chris. I managed to get it working about 30 seconds before you both replied ;-)

I put in the checked value for inches and ended up using if inches > 0 and it's now working perfectly.

Thanks for the help though, much appreciated.

Jonas: Yes, you were correct. That wasn't all of the code.