如何使用if-else条件检查停止无限while循环javascript

fjaof16o  于 2023-03-11  发布在  Java
关注(0)|答案(1)|浏览(164)

我不知道为什么这个程序不能成功运行,我真的很想跳出while循环,我已经检查了跳出的条件,但是当这些条件被满足时,循环仍然继续,并且无限运行。
我试过do-while循环,现在也试过while循环,但我还是不能让它工作。
求你了我需要人来帮我。

var smallestValue = function(n) {
    let q = 0,
    smallestvalue = 2,
    factors = [],
    stopme = true,
    stopme1 = true;
    while (stopme1) {
        console.log(stopme1 + '===')
        console.log(stopme + '====00')
        if (stopme && stopme1) {
            console.log(stopme1 + ' seee ooo ' + stopme)
            q = Math.floor(n / smallestvalue);

            console.log(n + ' = ' + smallestvalue + ' = ' + q)
            console.log(factors)
            if ((q === 1 && n === smallestvalue) && factors.length === 0) {
                console.log('aaaaaa')
                stopme = false;
                n = smallestvalue;
                factors = [];
                q = 1;
            }
            else {
                if (n % smallestvalue === 0 && q > 1) {
                    factors.push(smallestvalue);
                    n = q;
                    console.log(n + 'xxxx');
                    console.log(factors + 'xxx')
                    if (n % smallestvalue !== 0) {
                        smallestvalue++;
                    }
                }
                else {
                    console.log(smallestvalue + ' === ' + factors.length)
                    if (factors.length === 0) {
                        smallestvalue++;
                        q = Math.floor(n / smallestvalue);
                        console.log(smallestvalue + ' bbhhh')
                        console.log(n + ' ' + smallestvalue + ' ' + q + ' ' + factors.length)
                        if (q === 1 && n === smallestvalue && factors.length === 0) {
                            console.log("please stop me here");
                            stopme = false;
                            n = smallestvalue;
                            factors = [];
                            q = 1;
                        }
                    }
                    else {
                        console.log(factors)
                        factors.push(n);
                        console.log(factors)
                        let sum = 0;
                        for (let i = 0; i < factors.length; i++) {
                            sum += factors[i];
                        }
                        console.log(sum)
                        n = sum;
                        smallestvalue = 2;
                        factors = [];
                    }
                }
            }
        }
        else {
            stopme = false;
            stopme = false;
            stopme1 = false;
            stopme1 = false;
            stopme = false;
            n = smallestvalue;
            factors = [];
            q = 1;
        }
        stopme1 = stopme;
        console.log(stopme1)
        console.log(stopme)
        if (stopme === false && stopme1 === false) {
            console.log('i still want to stop here but no way');
            return;
            //break;
        }
    }
    return n;
};
kqlmhetl

kqlmhetl1#

添加行

break;

就在写着console.log("please stop me here");的那一行后面。
我将大胆地假设你总是希望你的函数返回一个数字,而不是undefined,我不清楚你的函数应该做什么,但是如果我的前一句话是真的,你可能还想在写console.log('i still want to stop here but no way');的行后面直接添加break;行。

相关问题