0

I have a problem with this code. when this jquery code will run, the "resulttry" is false but debugger shows me that return true for me!! that doesn't make sense!! my function should run the false function not else!! even in my backend code it shows me that 'resulttry' is false, so it MUST be run if with condition false but it will run else, run true condition!! can anyone help? my code:

if (resulttry = false) {
                $(".item-help").prop("disabled", true);
                interval = setInterval(moveItem, 100);

                $.ajax({
                    url: '/LuckyWheel/GetDataLuckywheel',
                    
                    success: function (result) {

                        var Award = result.Amount;
                        console.log(Award);
                        var items = new Array();

                        $('#box-lock .box').each(function () {
                            items.push(this.id);
                        });
                        var lengthId = items.length;
                        const arrayShow = [];
                        for (let i = 0; i < lengthId; i++) {
                            const randomIndex = Math.floor(Math.random() * items.length);
                            arrayShow.push(items[randomIndex]);
                            items.splice(randomIndex, 1);
                        }

                        $('.theme1').addClass("bg6");
                        $('.theme2').addClass("bg5");
                        $('.theme1').removeClass("select");
                        $('.theme2').removeClass("select");

                        $('.theme1').addClass("size-box");
                        $('.theme2').addClass("size-box");
                        for (var i = 0; i < arrayShow.length; i++) {
                            var item = arrayShow[i];
                            switch (item) {
                                case 'a':
                                    if ($('#amount1').text() == Award) {
                                        $('#a').removeClass("bg6");
                                        $('#b').addClass("award");
                                        clearInterval(interval);
                                    }
                                    break;
                                case 'b':
                                    if ($('#amount2').text() == Award) {
                                        $('#b').removeClass("bg6");
                                        $('#b').addClass("award");
                                        clearInterval(interval);
                                    }
                                    break;
                                case 'c':
                                    if ($('#amount3').text() == Award) {
                                        $('#c').removeClass("bg6");
                                        $('#c').addClass("award");
                                        clearInterval(interval);
                                    }
                                    break;
                                case 'd':
                                    if ($('#amount4').text() == Award) {
                                        $('#d').removeClass("bg6");
                                        $('#d').addClass("award");
                                        clearInterval(interval);
                                    }
                                    break;
                                case 'e':
                                    if ($('#amount5').text() == Award) {
                                        $('#e').removeClass("bg6");
                                        $('#e').addClass("award");
                                        clearInterval(interval);
                                    };
                                    break;
                                case 'f':
                                    if ($('#amount6').text() == Award) {
                                        $('#f').removeClass("bg6");
                                        $('#f').addClass("award");
                                        clearInterval(interval);
                                    }
                                    break;
                                case 'g':
                                    if ($('#amount7').text() == Award) {
                                        $('#g').removeClass("bg6");
                                        $('#g').addClass("award");
                                        clearInterval(interval);
                                    }
                                    break;
                                case 'h':
                                    if ($('#amount8').text() == Award) {
                                        $('#h').removeClass("bg6");
                                        $('#h').addClass("award");
                                        clearInterval(interval);
                                    }
                                    break;
                                default:
                                    $('.box').removeClass("bg3");
                                    $('.box').removeClass("select");
                                    $('.theme1').addClass("bg6");
                                    $('.theme2').addClass("bg5");
                                    break;
                            };
                        };

                        $(".item-help").prop("disabled", true);
                        toastr.success(result.Message);

                        setTimeout(function () {
                            $('.theme1').addClass("bg6");
                            $('.theme2').addClass("bg5");
                            $('.theme1').removeClass("award");
                            $('.theme2').removeClass("award");
                            $('.theme1').addClass("size-box");
                            $('.theme2').addClass("size-box");
                            $(".item-help").prop("disabled", false);


                        }, 4000);

                    }, error: function (xhr) {
                        $(".item-help").prop("disabled", false);
                        toastr.error(result.Message);
                    }

                });
            }
           else {

               $(".item-help").prop("disabled", true);
               toastr.error("استفاده شده");
           }
Ashkan AA
  • 33
  • 5
  • In the IF block you are using single = which always returns true. use == for compaision and delete the question. – Mehrwarz Apr 04 '22 at 06:05
  • @Mehrwarz to be picky, `a=x` returns the right side (x in that example) - so if the `if` is `if (x=0)` it will be false, not true. – freedomn-m Apr 04 '22 at 08:11
  • @freedomn-m no need to be picky. That's literally what's happening here `if (x = false) {} else {}` will always go inside the `else`. Which is what OP experiences. – VLAZ Apr 04 '22 at 08:39
  • No if you type (x=0) this will return true because you are checking if x can be set to 0, but if you type (x==0) it returns true if x is 0 or false or null and will return false if x is 1, String or other value. because you are comparing the x with 0. 1- if (resulttry = false) // aways return true 2- if (resulttry == false) // returns true if resulttry is false and false if it is true, 3- you can use if (! resulttry){} // runs the if block if resulttry is false. – Mehrwarz Apr 04 '22 at 19:44
  • @Mehrwarz "*No if you type (x=0) this will return true because you are checking if x can be set to 0*" this is completely incorrect. `x=0` is an assignment expression and the result is the assigned value. Therefore zero. Since it's falsy, you'd go to the `else`. Check here: https://jsbin.com/lewuzuf/edit?js,console also check the duplicate. – VLAZ Apr 04 '22 at 20:08
  • Your are right with the value of 0. ( x=0 returns 0 which is false). then you could always run else block not if block your code should start with if (resulttry == false) to execute first block if resulttry is false too. – Mehrwarz Apr 04 '22 at 20:29

0 Answers0