1
$(document).ready(function () {
    var t=true;
    var f=false;
    var cheap;
    $('.day1').on('change', function (e) {
        if($(this).val() == "Saturday"){
            cheap = true;
        }
        else{
            cheap=false;
        }
    });
    if(cheap==true){
        $('.pricing1').change(function () {
        var price = parseFloat($('.total').data('base-price')) || 0;
        $('.pricing1').each(function (i, el) {
            price += parseFloat($('option:selected', el).data('cheap'));
            $('.total').val('$' + price.toFixed(2));
        });
        //console.log('cheap',cheap)
        });
    }
    else{
        $('.pricing').change(function () {
        var price = parseFloat($('.total').data('base-price')) || 0;
        $('.pricing').each(function (i, el) {
            price += parseFloat($('option:selected', el).data('price'));
            $('.total').val('$' + price.toFixed(2));
        });
        console.log('cheap',cheap)
        });
    }

});

The console reading returns true for cheap when saturday is selected. but the if part is not executed. Every time only else part is executed. logically it should execute the if part if cheap is true. and the console displays the cheap value to true so the value of cheap is true. This is weird!

Saransh Seth
  • 77
  • 1
  • 1
  • 11

2 Answers2

2

You are registering the event handlers at the dom ready, at that point of time cheap has the value false so the if condition will not get satisfied so only the change handler in the else part will get registered.

$(document).ready(function () {
    var t = true;
    var f = false;
    var cheap;
    $('.day1').on('change', function (e) {
        if ($(this).val() == "Saturday") {
            cheap = true;
        } else {
            cheap = false;
        }
    });
    $('.pricing1').change(function () {
        if (cheap == true) {
            var price = parseFloat($('.total').data('base-price')) || 0;
            $('.pricing1').each(function (i, el) {
                price += parseFloat($('option:selected', el).data('cheap'));
                $('.total').val('$' + price.toFixed(2));
            });
            //console.log('cheap',cheap)
        } else {
            var price = parseFloat($('.total').data('base-price')) || 0;
            $('.pricing').each(function (i, el) {
                price += parseFloat($('option:selected', el).data('price'));
                $('.total').val('$' + price.toFixed(2));
            });
            console.log('cheap', cheap)
        }
    });

});

You can simplify the code to something like

$(document).ready(function () {
    var t = true;
    var f = false;
    var cheap;
    $('.day1').on('change', function (e) {
        if ($(this).val() == "Saturday") {
            cheap = true;
        } else {
            cheap = false;
        }
    });
    $('.pricing1').change(function () {
        var data = cheap ? 'cheap' : 'price';
        var price = parseFloat($('.total').data('base-price')) || 0;
        $('.pricing1').each(function (i, el) {
            price += parseFloat($('option:selected', el).data(data)) || 0;
        });
        $('.total').val('$' + price.toFixed(2));
    });

});
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
0

Try changing,

 if(cheap==true){

to

 if(cheap === true){

For explanation, have a look at this answer:

The == operator will compare for equality after doing any necessary type conversions. The === operator will not do the conversion, so if two values are not the same type === will simply return false. It's this case where === will be faster, and may return a different result than ==. In all other cases performance will be the same.

Community
  • 1
  • 1
Nandan Bhat
  • 1,573
  • 2
  • 9
  • 21