1

I'm attempting to solve the following problem from coderbyte.com:

Using the JavaScript language, have the function SimpleSymbols(str) take the str parameter being passed and determine if it is an acceptable sequence by either returning 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.

The following is my attempt:

function SimpleSymbols(str) { 

  // code goes here  
var abc = 'abcdefghijklmnopqrstuvwxyz';

for (var i = 0; i < str.length; i++) {
    if (abc.indexOf(str[i]) !== -1) {
        if (str[i-1] + str[i+1] === "++") {
            return true;
        }
        else {
            return false;
        }
    }
}

}

This works for the following cases:

SimpleSymbols("+a+d+"); // true 
SimpleSymbols("+ab+d+"); // false
SimpleSymbols("b+d+"); // false

The only case I have found where this doesn't provide the right answer is when there is a trailing letter, for example:

SimpleSymbols("+a+b"); // true

This returns true, when in fact it should return false.

NB: I'm assuming string will be lowercase... I haven't dealt with case sensitivity, but I'd like to get the lowercase version working and then I will make it case independent.

Any ideas on what is wrong with my code?

4

5 回答 5

1

我将其解释为:除了加号(包括无)之外,没有任何字母前面/后面没有字符:

function SimpleSymbols(str) { 
  return !/^[a-z]|[^+][a-z]|[a-z][^+]|[a-z]$/i.test(str)
}
于 2013-10-01T05:04:34.887 回答
1

您可以使用正则表达式:

    function SimpleSymbols(str) { 

      if (/^[a-zA-Z]/.test(str) || /[a-zA-Z]$/.test(str)) {
          return false;
      }
      else if (/[^+][a-zA-Z]/.test(str) || /[a-zA-Z][^+]/.test(str)) {
          return false;
      }
      else {
       return true; 
      }         
    }

或者

function SimpleSymbols(str) { 

  var arr = str.toLowerCase().split("");
  for (var i = 0; i < arr.length; i++) {
    if (arr[i] >= "a" && arr[i] <= "z") {
      if (i === 0 || i === arr.length) {
        return false;
      }

      if (arr[i-1] !== "+" || arr[i+1] !== "+") {
        return false;
      }
    }
  }

  return true; 

}
于 2019-05-20T08:43:15.200 回答
0

这将在由 + 符号包围的第一个成功字母上返回“true”,它不会继续检查到最后。

我还想知道字符串索引是否会在第一个/最后一个字符上[i-1]越界?[i+1]似乎没有,但我找不到语言参考。

更好的:

function SimpleSymbols(str) { 
    var abc = 'abcdefghijklmnopqrstuvwxyz';

    for (var i = 0; i < str.length; i++) {
        if (abc.indexOf(str[i]) !== -1) {
            if (str[i-1] + str[i+1] != "++") {
                return false;
            }
        }
    }
    return true;
}

可能还可以进行性能改进(通过 AND 而不是通过字符串添加检查边界字符),您也可以通过正则表达式来做到这一点。

通过正则表达式:

if (str.search( /[^+][a-z]/) >= 0)     // will match letters with no + beforehand.
    return false;
if (str.search( /[a-z][^+]/) >= 0)     // will match letters with no + afterward.
    return false;
return true;
于 2013-10-01T04:42:56.120 回答
0

我相信这是一个完美的正则表达式案例检查这里^[^a-z]*(\++[a-z]\++[a-z]?)*[^a-z]*$解释的正则表达式

像这样在javascript中使用它:

function SimpleSymbols(str) { 
   return !/^[^a-z]*(\++[a-z]\++[a-z]?)*[^a-z]*$/i.test(str);
}
于 2013-10-01T04:55:45.120 回答
0
export const simpleSymbols = (str) => {
    const arr = str.split('')
    let k = []
    arr.forEach(element => {
        if((/[a-zA-Z]/).test(element)){
            if(arr[arr.indexOf(element)-1]==="+" && arr[arr.indexOf(element)+1]==="+"){
                k.push(1)
            }else{
                k.push(0)
            }
        }
    }); 

    if(k.includes(0)){
        return false
    }else{
        return true
    }

}
于 2021-12-19T11:25:55.633 回答