如何跳出arf .find()

How To Break Out Of arr.find()?

本文关键字:find arf 何跳出      更新时间:2023-09-26

这可能不是最佳实践,但我想知道是否有可能打破arr.find()方法。

这里有一些代码,我正在工作,我已经重新做了一个循环,但我想知道为什么这是不允许的?

任何帮助都是感激的!

我明白这不是解决手头问题的最佳方法,我只是好奇为什么休息不像预期的那样工作,我在哪里搞砸了我的想法?

//Using the JavaScript language, have the function SimpleSymbols(str) take the str parameterbeing passed and determine if it is an acceptable sequence by either return in the string true or false. The str parameter will be composed of + and = symbols with several letters between them (ie. ++d+===+c++==a) and for the string to be true each letter must be surrounded by a + symbol. So the string to the left would be false. The string will not be empty and will have at least one letter.
//loop through Array
//Determine if letter is surrounded by +
//If yes continue on and return true  
//If no break loop and return false
function SimpleSymbols(str){
    str = str.split('');
    var pass = null;
    function finder(char){
        if (char.length === 1 && char.match(/[a-z]/i)){
            var firstPlus = str.indexOf(char)- 1;
            var secondPlus = str.indexOf(char)+ 1;
            console.log(str[firstPlus]);
            if (str[firstPlus] === '+' && str[secondPlus] === '+'){
                pass = 'true';
            } else {
                pass = 'false'
                break;
            }
        }
     }
    str.find(finder);
    return pass
 }
SimpleSymbols('++d+===+c++==a++q++');

以下代码将在5次迭代后打破循环:

SimpleSymbols('++-+d+===*c+3++==a++q-3++');
    function SimpleSymbols(str){
	  str = str.split('');
	  var pass = null;
	
	  str.find(finder);
	
	  function finder(char, i){
		console.log('Iteration No.' + i);
		if (str.indexOf(char) && char.length === 1 && char.match(/[a-z]/i)){
			var firstPlus = str.indexOf(char)- 1;
			var secondPlus = str.indexOf(char)+ 1;
			//console.log(str[firstPlus]);
			if (str[firstPlus] === '+' && str[secondPlus] === '+'){
				pass = 'true';
				return pass; 
			} else {
				pass = 'false';
			}
		 }
	  }
	  console.log('FINAL RESULT = ' + pass); 
	  return pass;
    }