如何实现循环额外条件

时间:2018-12-06 13:39:11

标签: javascript

我正在尝试实现一个非常简单的方法,但找不到正确的解决方案:如果在作用域中建立符号,如何替换字符串中的符号,如果不存在则如何使其保持原样。

    var BASE = "ABCDEFGHIJ"; 
    var CODE = "0123456789";
    var WORD = "DEKF" // K is out of range
    var CONS = []; // result expected: 34K5

    for (b=0; b<BASE.length; b++){

        for (w=0; w<WORD.length; w++){

            // if a sign of WORD is in BASE we change it with CODE equivalent: A->0
            if(WORD[w]==BASE[b]) {
                CONS.push(CODE[b]); 
            }

            // if not we keep it in place 
            else {
                // make sure the sign is out of BASE range
                if(BASE.search(WORD[w]) == -1 ) { 
                    CONS.push(WORD[w]); 
                    break;
                }
            }           
        }
    }

    console.log(CONS.join(''))

控制台

without the else condition = 345 ( no K )
else cond without break = KKK3K4KK5KKKK
else cond with break = KKK3K4KKKKKK
else without if->match and without break = DEKFDEKFDEKF3EKFD4KFDEK5DEKFDEKFDEKFDEKF
else without if->match and with break = DDD3EDDDDDD
with continue instead of break =  KKK3K4KK5KKKK

然后我尝试使用标签打破循环:

    for (b=0; b<BASE.length; b++){

        sign:
        for (w=0; w<WORD.length; w++){

            // if a sign of WORD is in BASE we change it with CODE equivalent: A -> 0
            if(WORD[w]==BASE[b]) {
                CONS.push(CODE[b]); 
            }

            // if not we keep it in place: 
            else {
                if(BASE.search(WORD[w]) == -1 ) { // make sure the sign is out of BASE range
                    CONS.push(WORD[w]); 
                    break sign;
                }
            }           
        }
    }

控制台

with sign label and break sign = KKK3K4KKKKKK
with sign label before the first loop and break sign; = K

3 个答案:

答案 0 :(得分:2)

提供替代方案。特别是如果列表变长并且出于可维护性考虑,可以使用Map而不是2个松散的字符串。 (在下面的示例中,地图是根据字符串创建的)

之后,您可以简单地用地图条目或角色本身替换所有角色:

const BASE = "ABCDEFGHIJ", CODE = "0123456789", WORD = "DEKF",
	codes = new Map([...BASE].map((b,i)=> [b,CODE[i]]));
    
let CONS = [...WORD].map(s=> codes.get(s) || s);

console.log(CONS.join(''));

答案 1 :(得分:1)

您需要外部循环来迭代WORD变量和内部循环 如果应该对BASE进行迭代,则您只需为WORD中的每个字母迭代一次

工作小提琴: (https://jsfiddle.net/3csxu8L6/1/

var BASE = "ABCDEFGHIJ";
var CODE = "0123456789";
var WORD = "DEKF" // K is out of range
var CONS = []; // result expected: 34K5



for (w = 0; w < WORD.length; w++) {
  for (b = 0; b < BASE.length; b++) {

    // if a sign of WORD is in BASE we change it with CODE equivalent: A->0
    if (WORD[w] == BASE[b]) {
      CONS.push(CODE[b]);
    }

    // if not we keep it in place 
    else {
      // make sure the sign is out of BASE range
      if (BASE.search(WORD[w]) == -1) {
        CONS.push(WORD[w]);
        break;
      }
    }
  }
}

alert(CONS.join(''));

答案 2 :(得分:0)

你想要这个吗?

var BASE = "ABCDEFGHIJ"; 
var CODE = "0123456789";
var WORD = "DEKF" // K is out of range
var CONS = []; // result expected: 34K5

Array.from(WORD).forEach(l => {

  let index = BASE.length
  let found = false
  
  while(--index >= 0) {
    if(BASE[index] === l) {
      CONS.push(index);
      found = true;
      break;
    }
  }

  if (!found) {
    CONS.push(l)
  }
  
} )

console.log(CONS) // [3,4,K,5]

这也是可能的非常快速的解决方案之一。