Donut Selector - Logical Operators Question

Hello,

I was wondering if someone could help me fix a small problem?

I created a website where you first select if you want to order 6 or 12 donuts from a dropdown
Next you select 6 or 12 donut flavors depending on what you chose in the quantity dropdown
If you select 6 donuts in the dropdown then choose 6 flavors the add to cart button enables.
If you select 12 donuts in the dropdown then choose 12 flavors the add to cart button enables.
Other then that the button should be disabled.

This works for 6 donuts and enables the button if I choose 6 flavors then if I chose anything more then 6 it disables like it should until I choose 12 flavors then it enables again but it should not because I only chose 6 donuts in the drop down not 12.

I feel I have my logical operators wrong.

Note: the donutAmount drop down values are set to 6 and 12 and stored in donutqty in the code.

$w.onReady(function () {
$w("#donut1").onChange(CartButtonEnableDisable);
$w("#donut2").onChange(CartButtonEnableDisable);

});

function CartButtonEnableDisable() {

//Get quantities from each donut flavor drop down and the amount of donuts you want to order dropdown

    let donut1=Number($w("#donut1").value);
    let donut2=Number($w("#donut2").value);
    let donutqty=Number($w("#donutAmount").value); 

//Add up the flavor selections

    let halfDozen = (donut1 + donut2)
    let fullDozen = (donut1 + donut2)

//If statement to enable button only if the drop down is 6 and the flavors chosen equal 6 or if the dropdown is 12 and the flavors chosen is 12.

    if((donutqty && halfDozen == 6) || (donutqty && fullDozen == 12)){
    $w('#buyNow').enable();
  } else $w('#buyNow').disable();

  console.log(halfDozen)
  console.log(fullDozen)
  console.log(donutqty)

}

Thanks in advance.

With logical operators like && they will execute each side of the operator before comparing them. What it is currently check is to see if donutqty is not null and if halfDozen/fullDozen is equal to the number. You could just see if the donutQty is equal to the sum of donut1 and donut2 (and therefore only need 1 sum of the flavor selections variable).

Thanks @meredithh I am getting proper calculations in the console log. When I select 6 in the donutqty dropdown I get six in the log then when I select the amount of each flavor it shows in the log as well so when it reaches 6 in the donutqty and 6 in the flavors it works then it triggers again when I hit 12 flavors even though the donutqty selector is still at 6. Not sure what to change my operator to so I can make sure it only allows 12 if donutyqty is set to 12 and 12 flavors are chosen but also still work with 6 in donutqty and 6 in flavors.

Thanks so much for getting back to me!

In this line

if((donutqty&&halfDozen==6)||(donutqty&&fullDozen==12))

donutqty is being checked to see if it is not null, not if it is equal to 6 or 12, since the expressions on either side of the and operator are evaluated first.

You can fix this by just either comparing donutqty to dozen and seeing if they are equal to each other (and therefore you only need one dozen variable, not the 2) or comparing donut value to 6 and 12 respectively. You should also being using triple equals, as double isn’t a true comparison.

Option 1:

if((donutqty === 6 && halfDozen===6)||(donutqty===12&&fullDozen===12))

Which will check if the total amount wanted equals a half dozen AND if the sum is equal to half dozen OR if total wanted quals a full dozen AND if the sum is equal to a full.

Option 2 (which I recommend):

if(donutqty === halfDozen)

As this will compare the sum of selected donut flavors to the total amount wanted in the first dropdown.

@meredithh thank you so much I am blown away this is such a great explanation and will not just solve this problem but will help me greatly in the future. I did not realize they worked this way.

Thank you so much for your help on this I went with your recommended option (Option 2) it is so simple yet genius.

Talk soon!