themightymanuel
themightymanuel

Reputation: 135

IFs coming back false

Why are the if's on lines 28 and 39 (if( clas == 1) and if( clas ==2 ) of this javascript always coming back false? If I replace the second with else. It gets executed every time. I have tried both == and === I have also tried putting the 1 and 2 as strings and both with and without the clas = parseInt(cla)

// JavaScript Document
var seat = new Array()
var fclass
var econ
var flight
var totseats

function init( seats, firclass, econo, flightnum )
{
    seat[0] = 'crew'
    fclass = firclass
    econ = econo
    flight = flightnum
    totseats = seats
    for( var count = 1; count <= seats; count++ )
    {
        seat[count] = 0;
    };
};

function reservation()
{
    var cla = document.getElementById('class').value;
    var name = document.getElementById('first').value;
    name += ' ';
    name += document.getElementById('last').value;
    clas = parseInt(cla)
    if( clas == 1 )
    {
        for( var seatnum = 1; seatnum < econ; seatnum++)
        {
            if( seat[seatnum] == 0 )
            {
                seat[seatnum] = name
                break;
            }
        }
    }
    if( clas == 2 )
    {
        for( var seatnum = econ; seatnum < totseats; seatnum++)
        {
            if( seat[seatnum] == 0 )
            {
                seat[seatnum] = name
                break;
            }
        }
    };
    pticket( name, seatnum );
};

function pticket( name, seatnum )
{
    document.getElementById('tname').innerHTML = name;
    document.getElementById('flight').innerHTML = 'Flight: ' + flight;
    document.getElementById('seat').innerHTML = seatnum;
    if( seatnum < econ )
    {
        document.getElementById('class').innerHTML = 'First Class';
    }
    else
    {
        document.getElementById('class').innerHTML = 'Economy';
    }
};

http://jsfiddle.net/FB26t/1/ p.s. the jsfiddle doesn't run the script at all but it does run properly as an actual webpage

Upvotes: 0

Views: 68

Answers (2)

plalx
plalx

Reputation: 43728

The element with id class is not your <select>. That's why. Your class <select> element has id select.

Also you should specify the radix parameter to the parseInt function, because the default is not 10 in all browsers.

//and do not forget the var statement if you want a local variable
var clas = parseInt(cla, 10); 

FIDDLE

Upvotes: 1

themightymanuel
themightymanuel

Reputation: 135

I had put document.getElementById('class').value but the corresponding element was actually document.getElementById('select')

Upvotes: 0

Related Questions