简体   繁体   English

while循环中的if else语句,其中if else语句确定while循环何时停止

[英]if else statement in while loop, where if else statement determines when while loop stops

var userInput = prompt("type something with or without double spacing");
var errorSpaces = [];
var maxSpaceCount = [];
var doneOnce = 0;
var done = 0;
var size;
var tempArray = [0, 0];
while (done === 0) {
    if (doneOnce === 0) {
        for (var i = 0; i<size; i++) {
            size = userInput.length - 1;
            if (userInput.substr(i, 2) == "  ") {
            userInput.replace(userInput.substring(i, j), userInput[i]);
            errorSpaces.push(0);
            }   
        }
        doneOnce = 1;
        maxSpaceCount.push(0);
    } else if (doneOnce === 1 && tempArray.length != 1) {
        for (var i = 0; i<size; i++) {
            tempArray = [0];
            size = userInput.length - 1;
            if (userInput.substr(i, 2) == "  ") {
                userInput.replace(userInput.substring(i, j), userInput[i]);
                tempArray.push(0);
            }
            doneOnce = 2;
        }
        maxSpaceCount.push(0);
    } else {
    done = 1;
    }
}
alert("done");

This loops at the second for loop rather than finishing. 这在第二个for循环而不是完成循环。 I know it probably isn't the best way to do it, but how could I make the 'else if' work so that when there are no more double spaces, it will go to the final else? 我知道这可能不是最好的方法,但是我如何才能使“ else if”工作,以便在没有更多的双倍空格时将其移到最后一个?

I am trying to eliminate any multiple spaces by iteratively replacing double spaces with single spaces, then re-reading to replace further double (previously triple) spaces, etc. 我正在尝试通过用单个空格迭代替换双空格,然后重新读取以替换其他双(以前是三)空格,以消除任何多个空格。

Way , way too much code if your goal is to replace any double (or more spaces) with a single space 如果您的目标是用单个空格替换任何双精度(或更多空格)的方法,那么方法太多

try regex 尝试正则表达式

var userInput = prompt("type something with or without double spacing");
userInput  = userInput.replace(/\s{2,}/g, ' ');
alert("done");

although not quite sure what you are trying to do with tempArray as it doesn't seem to make sense. 尽管不太确定您要使用tempArray做什么,因为这似乎没有意义。


EDIT 编辑

There appears to be some indication that there is a requirement to count how many occurrences of 2 or more spaces, so using the below will give you the count. 似乎有迹象表明,需要对2个或更多空格的出现次数进行计数 ,因此,使用以下内容可以计数。 The reason for the || ||的原因 bit is because if none are found, it will return null, || [] 该位是因为如果找不到,它将返回null, || [] || [] will change the null to empty array, so the length of it will be zero. || []会将null更改为空数组,因此其长度将为零。 Thanks to @RobG 感谢@RobG

var countOfMultipleSpaces = (userInput.match(/\s{2,}/g) || []).length;

I'm sure it goes without saying that you have to do this before you replace them all 我敢肯定,不用说,在更换所有组件之前必须先执行此操作

Is this what you want? 这是你想要的吗?

"type     something  with or   without    double spacing".replace(/\s{2,}/g, ' ');
//"type something with or without double spacing"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM